Reputation: 155
I currently have a standard hyperlink with two query strings to send to a PHP form seen below:
echo ' <strong>(<a id="cancel-upgrade" href="unsubscribe.php?cu='.$payRef.'&su='.$stripe_subscription_id.'">Cancel upgrade)</a></strong>';
The only way I know how to send data via AJAX is:
$.post('process-payment.php', $("form#payment-form").serialize(), function (data) {
if (data == "success") {
...something
else {
Something else
}
});
Is there anyway to use the link I have currently and use the query string data, via AJAX, to the PHP form and act on the success/error messages received?
Upvotes: 0
Views: 1118
Reputation: 2362
Use this code:
$(document).ready(function(){
$( "#cancel-upgrade" ).on( "click", function() {
$.ajax({
type: "POST",
url: "unsubscribe.php",
data: {cu:<?php echo $payRef; ?>, su:<php echo $stripe_subscription_id; ?>},
success: function(result){
}
});
});
});
Upvotes: 0
Reputation: 218798
You can add query string parameters to a POST request exactly as you would a GET request. For example:
$.post('process-payment.php?someKey=someValue&anotherKey=anotherValue', //...
So if you're echoing those values from PHP it might look like:
$.post('process-payment.php?cu=<?php echo $payRef; ?>&su=<?php echo $stripe_subscription_id; ?>', //...
(Or any of several ways to emit text to the PHP page.)
Upvotes: 1
Reputation: 721
Try this
$.ajax({
type: "POST",
url: "ajax_file.php",
data: 'param1=value1¶m2=value2',
success: function(result){
}
});
Upvotes: 0