-1

我需要发出与以下 PHP 代码具有相同功能的 JQuery AJAX 请求。

问题:

我如何将 PHP 变量(merchantId)传递给 AJAX?

我如何处理成功功能?

PHP代码:

    <form method="get" action="http://www.example.com/payments/paymentStore.html">
    <input type="hidden" name="merchantId" value="<?php=$merchantid?>">
    <input type="submit" value="Pay" name="PayKZM">
    </form>

阿贾克斯调用

<div class="buttons">
  <div class="right">

<input type="button" value="<?php echo $button_confirm; ?>" id="button-confirm" class="button" />
  </div>
</div>
<script type="text/javascript"><!--
$('#button-confirm').bind('click', function() {
    $.ajax({ 
        type: 'GET',
        url: 'http://www.example.com/payments/paymentStore.html',
        data: "merchantId="+$merchantId,
        success: function() {
            location = '<?php echo $continue; ?>';
        }       
    });
});
//--></script> 
4

2 回答 2

1

您非常接近,您只需要从隐藏的输入字段中获取值:

HTML:

<input type="hidden" id="merchantId" name="merchantId" value="<?php=$merchantid?>">

JS:

$('#button-confirm').bind('click', function() {
    var merchant_id = $('#merchantId').val();
    $.ajax({ 
        type: 'GET',
        url: 'http://www.example.com/payments/paymentStore.html',
        data: "merchantId="+merchant_id,
        success: function() {
            location = '<?php echo $continue; ?>';
        }       
    });
});

当然,看看你通过 PHP 字符串将位置传递给 JS 部分的事实,你可以简单地做

...
data: "merchantId='<?php echo $merchantId; ?>'",
...
于 2013-03-12T05:54:59.190 回答
0

试试这个

  <div class="buttons">
  <div class="right">

 <input type="button" value="<?php echo $button_confirm; ?>" 
   id="button-confirm" class="button" onclick='functionname(".$merchantid.")' />
 </div>
</div>
 <script type="text/javascript"><!--
  public functionname(merchan_id) {
$.ajax({ 
    type: 'GET',
    url: 'http://www.example.com/payments/paymentStore.html',
    data: "merchantId="+merchan_id,
    success: function() {
        location = '<?php echo $continue; ?>';
    }       
});
});
 </script> 
于 2013-03-12T05:28:57.760 回答