Problem :
addToAuthorizeform()
;$amount
. This variable should show the output of the value1
that is calculated by Ajax Function.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?
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.