potts
potts

Reputation: 155

Send AJAX Request with Query String

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

Answers (3)

Hirdesh Vishwdewa
Hirdesh Vishwdewa

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

David
David

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

Ravi Patel
Ravi Patel

Reputation: 721

Try this

$.ajax({
        type: "POST",
        url: "ajax_file.php",
        data: 'param1=value1&param2=value2',
        success: function(result){

            }
    });

Upvotes: 0

Related Questions