当用户单击链接时,它使用jquery ajax提交表单以转到paypal。由于某种原因,它不起作用。感谢您的帮助。

HTML链接:

<a href="#" onClick="javascript:go_paypal();">Paypal</a>


JS函数:

function go_paypal() {

data = 'req_paypal=1';

$.blockUI({ message: '<h1> Going to Paypal...</h1>',css:{background:'#000'} });
    $.ajax({
      type: "POST",
      url: "index.php",
      data: data,
      success: function(data) { $("#paypal_form").html(data); $("#payPalForm").submit(); } ,
      error: function() {$.unblockUI(); alert('Unable to communicate to server.'); }
    });
    return false;
}


PHP代码:

if(isset($_POST['req_paypal']) && $_POST['req_paypal'] == 1 ) {


    $sql = 'INSERT INTO `transactions` (id,type,ip,time,ammount,status) VALUES (NULL,1,\''.$_SERVER['REMOTE_ADDR'].'\',\''.time().'\',\''.$global['paypal_prod_amount'].'\',0) ';
    echo $sql;
//      $sql2 = 'INSERT INTO `users` (id,email,password,referred_by,referrals) VALUES ('',)';

    mysql_query($sql);

    $id = mysql_insert_id();

    $html = '
    <form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" id="payPalForm">
<input type="hidden" name="item_number" value="One Year of Imgur Pro">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="business" value="'.$global['paypal_email'].'">
<input type="hidden" name="custom" value="'.base64_encode($id).'">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="return" value="'.$global['paypal_return'].'">
<input name="item_name" type="hidden" id="item_name" value="One Year of Imgur Pro" >
<input name="amount" type="hidden" id="amount" value="'.$global['paypal_prod_amount'].'" >
</form>
    ';

    echo $html;exit;
}

最佳答案

您的数据对象必须是json对象,而不是字符串。

尝试这个:

function go_paypal() {
    data = { req_paypal: 1 };

    $.blockUI({ message: '<h1> Going to Paypal...</h1>', css:{background:'#000'} });
        $.ajax({
          type: "POST",
          url: "index.php",
          data: data,
          success: function(data) { $("#paypal_form").html(data).submit(); } ,
          error: function() { $.unblockUI(); alert('Unable to communicate to server.'); }
        });
}


我也建议将标签更改为

$("#go_paypal").click(function() { go_paypal(); }


要么

<a href="javascript:go_paypal();">Paypal</a>


这样,单击链接时页面不会滚动到顶部

07-27 21:15