0

I wrote code below that is working perfectly for displaying the results of my sales tax calculation into a span tag. But, I am not understanding how to change the "total" value into a variable that I can work with.

<script type="text/javascript">
    function doStateTax(){
        var grandtotalX = $('#GRANDtotalprice').val();
        var statetaxX = $('#ddl').val();

        $.post('statetax.php',
        {statetaxX:statetaxX, grandtotalX:grandtotalX}, 
        function(data) {
        data = $.parseJSON(data);
        $('.products-placeholder').html(data.products);     
        $('.statetax-placeholder').html(data.statetax);     
        $('.total-placeholder').html(data.total);                               
        // ...
        });
        return false;
    };
</script>   

Currently, $('.total-placeholder').html(data.total); is successfully placing the total number into here:

<span class="total-placeholder"></span> 

but how would I make the (data.total) part become a variable? With help figuring this out, I can pass that variable into a hidden input field as a "value" and successfully give a proper total to Authorize.net

I tried this and id didn't work (see the testtotal part to see what I'm trying to accomplish)..

function(data) {
    data = $.parseJSON(data);
    $('.products-placeholder').html(data.products);     
    $('.statetax-placeholder').html(data.statetax);     
    $('.total-placeholder').html(data.total);       
    $testtotal = (data.total);                      
    // ...

3 Answers 3

1

If you are using a hidden field inside a form, you could do:

//inside $.post -> success handler.
$('.total-placeholder').html(data.total);
$('input[name=yourHiddenFieldName]', yourForm).val(data.total);

This will now be submitted along with the usual submit. Or if you want to access the data elsewhere:

var dataValue = $('input[name=yourHiddenFieldName]', yourForm).val();
Sign up to request clarification or add additional context in comments.

2 Comments

This is very close to the answer. The problem with posting to a hidden field "directly" is that Authorize.net sets a variable before the hidden field, runs it through a SIM fingerprint.. does some stuff.. and then places the total into a hidden input value as a variable. Their code says $amount_var = 'yourtotal'... and I want to somehow put $amount_var = 'variable we set in the success handler' I'm sorry I'm not explaining this well.
I do not know about other parts of your code, but why not set $amount_var=data.total; after all other sources modify that variable?
0

The "data" object you are calling can be used anywhere within the scope after you have a success call. Like this:

$.post('statetax.php',
{statetaxX:statetaxX, grandtotalX:grandtotalX}, 
function(data) {
    data = $.parseJSON(data);

    var total = data.total;
    var tax = data.total * 0.19;
});

return false;
};

Whenever you get an object back always try to see with an alert() or console.log() what it is.

alert(data); // This would return <object> or <undefined> or <a_value> etc.

After that try to delve deeper (when not "undefined").

alert(data.total); // <a_value>?

1 Comment

When I try this and place <?php echo $total;?> into my page, it doesn't show the total at all.
0

If you want 'testotal' to be recognized outside the function scope, you need to define it outside the function, and then you can use it somewhere else:

 var $testtotal;
 function(data) {
                data = $.parseJSON(data);
                $('.products-placeholder').html(data.products);     
                $('.statetax-placeholder').html(data.statetax);     
                $('.total-placeholder').html(data.total); 
                $testtotal = (data.total);

EDIT:

The comments are becoming too long so i'll try and explain here:

variables defined in javascript cannot be accessed by PHP and vice versa, the only way PHP would know about your javascript variable is if you pass it that variable in an HTTP request (regular or ajax).

So if you want to pass the $testtotal variable to php you need to make an ajax request(or plain old HTTP request) and send the variable to the php script and then use $_GET/$_POST to retrieve it.

Hope that answers your question, if not then please edit your question so it'll be clearer.

5 Comments

It tried this too and when I did <?php echo $testtotal;?> it showed nothing at all. Although I know for certain (data.total) has a number that is being sent to the .total-placeholder. So I know the value has to be there but it's not setting as a variable properly.
Basically the page is a shopping cart. The cart updates in real-time when you select your state (to figure if you owe sales tax or not) so I have the php page using json to send over the total, state, etc. to a php page to do all the calculations. Then it sends the grand total back to the success callback (not sure of the exact terminology) and I'd like to set that as a variable. Authorize.net requires I set the total to a variable and pass it that way.
It is unclear what your problem is, you can't send it to the php in the ajax or you can't display it once the ajax callback returned?
Once the callback is returned, I can see it. I can use $('.total-placeholder').html(data.total); to actually get it to show up in a <span> tag with class="total-placeholder". That works. What I don't understand is how to make that into a variable. Authorize.net has code that says $amount_var = 'put your total here'. The only way I know how to explain is by typing some code that is obviously wrong but shows what I need. ---> $amount_var = echo "<span class='total-placeholder'></span>" but of course that's probably not how it is supposed to be done.
I guess I just need to know the syntax for placing the callback (data.total) as a variable.

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.