0

Problem :

  1. There is a Value1 that is calculated from Ajax Function addToAuthorizeform();
  2. There is a Value2 that is a PHP varibale $amount. This variable should show the output of the value1 that is calculated by Ajax Function.
  3. How it is possible?

My code so far:

//AJAX FUNCTION THAT OUTPUTS AN AMOUNT 
//SEE LINE 24 value="'+arrData[1]+'" <-- This is the correct value that needs to be //output on PHP VARIABLE

<script>
    function addToAuthorizeForm() {
        $wbc('#slots_purchased').html('');
        var new_html = '';
        var i = 1;
        $wbc('#booking_slots').find('input').each(function () {
            if ($wbc(this).attr('checked')) {
                var slot_id = $wbc(this).val();
                //ajax request to get data
                $wbc.ajax({
                    url: '<?php echo plugins_url('
                    my_plugin / public ');?>/ajax/getSlotInfo.php?slot_id=' + $wbc(this).val(),
                    success: function (data) {

                        arrData = data.split("$");
                        if (arrData[1] > 0) {
                            q = 1;
                            if ($wbc('#seats_' + slot_id).val() != undefined) {
                                q = $wbc('#seats_' + slot_id).val();
                            }
                            new_html += '<input type="hidden" name="x_amount_' + i + '" value="' + arrData[1] + '" />';
                            $wbc('#slots_purchased').html(new_html);
                            i++;
                        }
                    }
                });

            }

        });

    }
</script>

Now The PHP Variable is

$amount = '';

Now I need to know what code should I put after $amount = 1 so I can call or echo the Ajax same value '+arrData[1]+' that is calculated on line 24 of the Javascript Function.

Here is the Authorize.net HTML form that i am using to Submit.

<?php
require_once 'anet_php_sdk/AuthorizeNet.php'; // Include the SDK you downloaded in Step 2
$fname = $bookingReservationObj->getReservationName();
$api_login_id = $bookingSettingObj->getAuthorizeAPI();
$transaction_key =  $bookingSettingObj->getAuthorizeTXN();
$amount = // I am not sure what to put here to call Ajax value that i need answer
$fp_timestamp = time();
$fp_sequence = "123" . time(); // Enter an invoice or other unique number.
$fingerprint = AuthorizeNetSIM_Form::getFingerprint($api_login_id,
  $transaction_key, $amount, $fp_sequence, $fp_timestamp)
?>

    <!-- authorize.net form -->
    <form action='https://test.authorize.net/gateway/transact.dll' METHOD='POST' name="authorize_form" style="display:inline">

        <!-- Authorize Configuration -->

        <input type='hidden' name="x_login" value="<?php echo $api_login_id ?>" />
        <input type='hidden' name="x_fp_hash" value="<?php echo $fingerprint?>" />
        <input type='hidden' name="x_fp_timestamp" value="<?php echo $fp_timestamp?>" />
        <input type='hidden' name="x_fp_sequence" value="<?php echo $fp_sequence?>" />
        <input type='hidden' name="x_version" value="3.1">
        <input type='hidden' name="x_show_form" value="payment_form">
        <input type='hidden' name="x_test_request" value="true" />
        <input type='hidden' name="x_method" value="cc">
        <input type='hidden' name="x_first_name" value="<?php echo $fname ?>">
        <input type='hidden' name="x_last_name" value="<?php echo $fname ?>">
        <input type='hidden' name="x_email" value="<?php echo $fname ?>">
        <input type='hidden' name="x_phone" value="<?php echo $fname ?>">
        <input type='hidden' name="x_description" value="<?php echo 'Cruzz Booking '; ?>">


       <!--slots purchased-->
        <div id="slots_purchased">

        </div>

        <input type='hidden' name="x_receipt_link_method" value="link">
        <input type='hidden' name="x_receipt_link_text" value="Click here to return to our home page">
        <input type='hidden' name="x_receipt_link_URL" value="<?php echo site_url('')."/?p=".$post->ID."&authorize_confirm=1"; ?>">
        <input type="hidden" name=" x_cancel_url" value="<?php echo site_url('')."/?p=".$post->ID; ?>">
        <input type="hidden" name="rm" value="POST">



    </form>

How should I start?

Simon John Hd
  • 25
  • 1
  • 10
  • how you get anything when you have empty string in $amount ... – NullPoiиteя Jun 13 '13 at 01:38
  • what are you doing with that $amount variable? – Kylie Jun 13 '13 at 01:40
  • i dont know what to write after $amount so i can get the value of Ajax call, this is why i am asking, Thanks – Simon John Hd Jun 13 '13 at 01:40
  • But if the page is already loaded, you can't assign a variable, so my question is this again.....what are you wanting to do with that $amount variable – Kylie Jun 13 '13 at 01:41
  • @Kylek $amount variable is Authorize.net amount, it is required because Authorize.net uses this variable output to calculate fingerprint. – Simon John Hd Jun 13 '13 at 01:41
  • ok, so when is it used? before or after your ajax? On next page? when? – Kylie Jun 13 '13 at 01:41
  • What I mean, is youre either gonna have to hide it in the DOM or store as a cookie, to be able to use it on the next page.....but the real question remains....when is $amount used? I guess what Im trying to say is, if you need it on THIS page, you're out of luck, you can't assign php variable after the request has already been made.....the only option then, if you need it on this page, is to assign it to javascript, and then do your Authorize through ajax as well – Kylie Jun 13 '13 at 01:44
  • Show PHP code with `$amount`. – furas Jun 13 '13 at 01:45
  • $amount is used when the button is pressed. – Simon John Hd Jun 13 '13 at 01:48
  • AJAX runs on the client, PHP runs on the server. You can't set server variables from the client, except by performing another AJAX call. – Barmar Jun 13 '13 at 01:49
  • @Barmar how can i perform another Ajax call, can you please help. Kylek i have to echo $amount such as so form can be processed but i need $amount = value calculated from Ajax function in my question – Simon John Hd Jun 13 '13 at 01:52
  • It's not clear what you're really trying to do. You're already putting the amount into the form with Javascript, why do you think you need to do it with PHP? PHP can only output whole pages, it can't update parts of the existing page, you do that with Javascript. – Barmar Jun 13 '13 at 01:55
  • Well if the variable is already there i your ajax you have it....you don't need $amount to insert into form....infact you are already inserting it into the input already....unless you need it in another field, then thats easy, just provide the html of the field you need to put it in, and Ill give you code – Kylie Jun 13 '13 at 01:56
  • Are you gonna give me the element you wanna insert it into or not??? – Kylie Jun 13 '13 at 01:59
  • ok let me make it clear again, If i dont use $amount, i can not accompish the form to submit to Authorize.net Payment gateway, that receive the amount from $amount variable, If you check the code above, You will see new_html is what submit the value calculated from Ajax. I need to output this value using PHP variable $amount. Thanks – Simon John Hd Jun 13 '13 at 02:00
  • God probably just abandonned us... – lukaleli Jun 13 '13 at 02:03
  • I understand what youre trying to do, but you dont need to give it to $amount......if you need it in your form, to submit to the other side....you ALREADY HAVE IT from that ajax call.....so lets use it.......in fact unless you provide WHERE that $amount is being used, nobody can help you – Kylie Jun 13 '13 at 02:03
  • @Kylek i updated the HTML form that is being output – Simon John Hd Jun 13 '13 at 02:04
  • @Kylek if i dont use $amount variable, $fingerprint will not be generated and it returns error. thats the case. If you know any other method please help – Simon John Hd Jun 13 '13 at 02:05
  • Does AuthorizeNetSIM_Form::getFingerprint($api_login_id, $transaction_key, $amount, $fp_sequence, $fp_timestamp) rely on the $amount? – Kylie Jun 13 '13 at 02:08
  • @Kylek Yes you can see that on code. that is why i want to assign ajax value to $amount – Simon John Hd Jun 13 '13 at 02:10
  • Ok, well the answer is you can't....but I have a feeling you're doing something backwards here – Kylie Jun 13 '13 at 02:10
  • Why are you using ajax for the amount in the first place? Can this not be calculated on the server? – Kylie Jun 13 '13 at 02:11
  • @Kylek because the user have to select number of Quantities before he submit form, and the best method to calculate this is Ajax only – Simon John Hd Jun 13 '13 at 02:13
  • Well the only solution I see, is that when you do that ajax call to get the amount, at that point you move that AuthorizeNetSIM_Form::getFingerprint($api_login_id, $transaction_key, $amount, $fp_sequence, $fp_timestamp) into that page, and return both the amount and figerprint at the same time – Kylie Jun 13 '13 at 02:15
  • But you will have to pass, the $api, $transaction_key, $fp_sequence, and timestamp to the ajax as well....its a complete restructuring on the other side for you – Kylie Jun 13 '13 at 02:16
  • i am getting your point @Kylek but can you write the code and show example please – Simon John Hd Jun 13 '13 at 02:17
  • No unfortunately not, this is a complex job.....I can do it for you, but I charge $50/hr, which I doubt you feel like paying, for something that you can probably figure out with time – Kylie Jun 13 '13 at 02:18
  • I would need to see all the other code, for example...the code for plugins_url('my_plugin / public ');?>/ajax/getSlotInfo.php? – Kylie Jun 13 '13 at 02:19
  • thank you for help, your doubt is true, Not able to pay. Thanks again – Simon John Hd Jun 13 '13 at 02:20
  • No problem. Hopefully you get what Im trying to say....when I say, put that Authorize function on the other side, in the getSlotInfo.php file, and in your ajax, you will pass those other variables – Kylie Jun 13 '13 at 02:26

1 Answers1

0

In your code $wbc must be your jQuery Object, it's normally just $. If for some reason $wbc does not refer to the jQuery Object you have a problem. ajax is a method of the jQuery Object. The ajax method takes an Object Literal as its argument. A JavaScript Object Literal is really an Associative Array. url is a property of the Object You are passing in as an argument. The value for that property is '<?php echo plugins_url('my_plugin/public');?>/ajax/getSlotInfo.php?slot_id='+ $wbc(this).val(), which you must be running through your server, so this must be a .php file. To use plugins_url() you must be using WordPress.

You are using the $wbc.ajax({type:'GET'}) method so additional information can be sent like 'getSlotInfo.php?slot_id='+$wbc(this).val()+'&anotherProperty=anotherValue. So the & seperates properties.

See where your code says getSlotInfo.php?slot_id=? The slot_id part can be accessed with $_GET['slot_id'] on the page your url is sending information to, which happens to be getSlotInfo.php. You can use <?php $varHere = $_GET['slot_id'] ?> on your getSlotInfo.php page to create a PHP variable that holds jQuery.

If you had a dataType: 'json' in your ajax method Object argument, like $wbc.ajax({dataType: 'json'}), then you could use PHP to generate JavaScript Object Notation, which is an Associative Array. The PHP method of choice for this on your getSlotInfo.php page will be json_encode(). As long as you print or echo json_encode() with PHP, when you have a successful response the data argument of $wbc.ajax({success: function(data){}}) will hold the Associative Array, which can be used with JavaScript's for in loop like:

for(var i in data){
  var property = i;
  var value = data[i];
}

Your PHP, on getSlotInfo.php, that sends to this JavaScript Object Literal might look like:

<?php
  if(isset($_GET['slot_id']) && isset($_GET['anotherProperty'])){
    $ary = array('prop1' => $_GET['slot_id'], 'prop2' => $_GET['anotherProperty']);
    echo json_encode($ary);
  }
  else{
    header('LOCATION: urlOfChoice.html');
  }
?>

Using this methodology there is no reason to split the data since its not a string response. Instead it's already JSON.

This might help you understand the post method Jquery AJAX post to PHP.

Community
  • 1
  • 1
StackSlave
  • 10,613
  • 2
  • 18
  • 35