0

$newprice = 100;

<?php echo "Rs." .$newprice-= ."<div id = 'discountamt'></div>";

In the above code i trying to subtract two values. One is $newprice and second is value of discountamt which is comes from ajax.

when i concatenate $newprice with this <div id = 'discountamt'></div> using concatenate operator (.). It is showing error.

I tried this code in which i used space between them.....but not working

<?php echo "Rs." .$newprice-= ''."<div id = 'discountamt'></div>";

i want

100 (newprice) - 10 (discountamt)

.. output should be 90

3
  • You are strying to subtract a string, what do you expect? Commented Aug 30, 2014 at 13:04
  • 4
    You cant mix PHP on your page, and ajax which occurs later after the page is loaded (and the PHP has already run) Commented Aug 30, 2014 at 13:05
  • why not send that value along with your AJAX, then process the values from the server then respond with the end result, then use that response to change the values on the front again Commented Aug 30, 2014 at 13:07

2 Answers 2

1

If the 'dicountamt' div is being populated by AJAX, then PHP will not be able to do this calculation as PHP runs server side and has already finished executing.

You would most likely need to do this calcultaion in javascript when your AJAX call returns. We don't have enough of your HTML/JS to help you all the way, but the way you would get the number inside the 'discountamt' div would look like this:

var discountamt = parseInt(document.getElementById("discountamt").innerHTML)
Sign up to request clarification or add additional context in comments.

Comments

0

I got my solution....demo

<?php
    $newprice = 100;
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        $return_value = $newprice - $_POST['discountamt'];
        echo json_encode($return_value);
        exit;
    }

    ?>

    Total: <div id="total">0</div><br/>
    Discount: <div id='discountamt'>10</div><br/>
    <button type="button" id="calculate">Calculate</button>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript">  
    $(document).ready(function(){   
        $('#calculate').on('click', function(){
            $.ajax({
                url: document.URL,
                data: {discountamt: $('#discountamt').text()},
                type: 'POST',
                dataType: 'JSON',
                success: function(response) {
                    $('#total').text(response);
                }
            });
        })
    });
    </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.