使用查询字符串发送AJAX请求

时间:2015-06-26 09:25:46

标签: php jquery ajax

我目前有一个带有两个查询字符串的标准超链接,可以发送到下面的PHP表单:

echo ' <strong>(<a id="cancel-upgrade" href="unsubscribe.php?cu='.$payRef.'&su='.$stripe_subscription_id.'">Cancel upgrade)</a></strong>';

我知道如何通过AJAX发送数据的唯一方法是:

$.post('process-payment.php', $("form#payment-form").serialize(), function (data) {
    if (data == "success") {
        ...something
    else {
        Something else
    }
});

是否有使用我当前的链接并通过AJAX使用查询字符串数据到PHP表单并根据收到的成功/错误消息进行操作?

3 个答案:

答案 0 :(得分:1)

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.)

答案 1 :(得分:0)

Try this

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

            }
    });

答案 2 :(得分:0)

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){

            }
    });
});
});