中断表单帖子,执行jquery $ .post

时间:2015-03-01 20:33:06

标签: php jquery ajax json forms

我正试图解决我想解决的问题。我有一个表格,我收集人口统计信息和付款金额。然后,我需要以浏览器不可见的方式计算哈希,序列和时间戳,然后使用这些计算字段更新隐藏的表单值,然后提交给付款处理器。

我目前正在尝试的是使用带有输入和隐藏字段的表单的主页面,当单击提交时,运行jquery函数,将ajax发布到使用数量输入的计算php中,是哈希的一部分。计算php计算项目然后传回json数组。

然后我要解析返回,更新隐藏的表单值,然后提交。

我将在下面附上我的代码,但是现在,我正在打断帖子,将值传递给计算php并构建数组。此时,我的进程停止,似乎我没有返回主页更新值并发布。

我做错了什么?此外,如果您有更好的解决方案,我很乐意听到。感谢

主页:

    <!DOCTYPE html>
<html>
<head>
<script     src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(document).ready(

        function () {

            $('#Waterform').submit(

                function(event) {

      //var txt = $("x_amount").val();


    var formData = { xamount : $('#x_amount').val() }; 
     $.ajax({
  type: 'POST',
  url: "calc_fp_etc_anet.php",
  data: formData,
  success: function(result,stat,xmlRegObj){


      for (var key in result)
{
   if (result.hasOwnProperty(key))
   {

      $('#returnmsg').val(result[key].type);
      $('#x_fp_hash').val(result[key].fp_hash);
      $('#x_fp_sequence').val(result[key].fp_sequence);
      $('#x_fp_timestamp').val(result[key].fp_timestamp);
   }
}

      //return false;
      $("#Waterform").submit();},
  error: function (jqXHR, textStatus, errorThrown){
      $('#returnmsg').val(textStatus);
      return false;},
  dataType: "json",
  async:false
});

                }                                                

            )

        }


    );
</script>
</head>
<body>


<form id="Waterform" name="adaWaterform" action = 'https://secure.authorize.net/gateway/transact.dll' method = 'post'>

                    <input type = 'hidden' id = 'x_show_form' name = 'x_show_form' value = 'PAYMENT_FORM' />
                    <input type = 'hidden' id = 'x_description' name = 'x_description' value = 'Online Bill Payment' />
                    <input type = 'hidden' id = 'x_login' name = 'x_login' value = 'xxx4NhDyy' />
                    <input type = 'input' id = 'x_fp_hash' name = 'x_fp_hash' value = '' />
                    <input type = 'input' id = 'x_fp_sequence' name = 'x_fp_sequence' value = '' />
                    <input type = 'input' id = 'x_fp_timestamp' name = 'x_fp_timestamp' value = '' />
                    <input type = 'input' id = 'returnmsg' name = 'returnmsg' value = '' />

            <div id="m_divLineItems">
              <h3>Service Information</h3>
              <div id="ContentBodyPlaceholder_m_divTime"></div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblAccountNumber" class="InfoLabel">
                  Account Number</label>
                <input type = 'text' id = 'x_invoice_num' name = 'x_invoice_num' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblPhone" class="InfoLabel">
                  Phone</label>
                <input type = 'text' id = 'x_phone' name = 'x_phone' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblEMail" class="InfoLabel">
                  e-mail</label>
                <input type = 'text' id = 'x_email' name = 'x_email' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblServiceAddress" class="InfoLabel">
                  Service Address</label>
                <input type = 'text' id = 'x_ship_to_address' name = 'x_ship_to_address' value = '' />
              </div>
              <h3>Billing Information</h3>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblFirstName" class="InfoLabel">
                  First Name</label>
                <input type = 'text' id = 'x_first_name' name = 'x_first_name' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblLastName" class="InfoLabel">
                  Last Name</label>
                <input type = 'text' id = 'x_last_name' name = 'x_last_name' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblAddress" class="InfoLabel">
                  Address</label>
                <input type = 'text' id = 'x_address' name = 'x_address' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblCity" class="InfoLabel">
                  City</label>
                <input type = 'text' id = 'x_city' name = 'x_city' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblState" class="InfoLabel">
                  State</label>
                <input type = 'text' id = 'x_state' name = 'x_state' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblZip" class="InfoLabel">
                  Zip</label>
                <input type = 'text' id = 'x_zip' name = 'x_zip' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblAmount" class="InfoLabel">
                  Amount</label>&nbsp;
                <input type = 'text' id = 'x_amount' name = 'x_amount' value = '' />
                <input type="submit" class="submit" id="submitbutton" name="submitbutton" value="Pay Bill &gt;&gt;"/>
                </div>
    </div>





</form>


</body>
</html>

PHP计算:

<?php

require_once (dirname(__FILE__).'./config.php');

$amount = $_POST['xamount'];


  $loginID      = AUTHORIZENET_API_LOGIN_ID;
  $transactionKey = AUTHORIZENET_TRANSACTION_KEY;


  // a sequence number is randomly generated
  $sequence = rand(1, 10000);
  // a timestamp is generated
  $timeStamp    = time();


  $x_fp_hash = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey);

  $x_fp_sequence = $sequence;

  $x_fp_timestamp = $timeStamp;


$result = json_encode(array("type"=>"success","fp_hash"=>"$x_fp_hash","fp_sequence"=>"$x_fp_sequence","fp_sequence"=>"$x_fp_sequence","fp_timestamp"=>"$x_fp_timestamp"));

echo $result;

die();

?>

1 个答案:

答案 0 :(得分:0)

终于想通了!由于某种原因,.post最后不喜欢“json”声明,即使返回是JSON。然后我不得不使用parseJSON将值获取到数组中。然后,我更新了隐藏的值,解除了提交和提交的表单。工作得很好。需要javascript ...但它确实有效。

主页:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(document).ready(

        function () {

            $('#adaWaterform').submit(

                function(event) {
                    event.preventDefault();      

        $.post("calc_fp_etc_anet.php",{ xamount : $('#x_amount').val() }, function(result){
            //alert("Data: " + result);
            var json_obj = $.parseJSON(result);

            //alert( json_obj.fp_hash);



                   $('#x_fp_hash').val(json_obj.fp_hash);
                   $('#x_fp_sequence').val(json_obj.fp_sequence);
                   $('#x_fp_timestamp').val(json_obj.fp_timestamp);


                   $('#Waterform').unbind("submit").submit(); 


   });//function result



                }//function event                                                
              //$("#Waterform").submit();   
            )//submit

        }//function


    );//ready
</script>
</head>
<body>


<form id="Waterform" name="Waterform" action = 'https://secure.authorize.net/gateway/transact.dll' method = 'post'>

                    <input type = 'hidden' id = 'x_show_form' name = 'x_show_form' value = 'PAYMENT_FORM' />
                    <input type = 'hidden' id = 'x_description' name = 'x_description' value = Online Bill Payment' />
                    <input type = 'hidden' id = 'x_login' name = 'x_login' value = 'xxx4Nhyyy' />
                    <input type = 'hidden' id = 'x_fp_hash' name = 'x_fp_hash' value = '' />
                    <input type = 'hidden' id = 'x_fp_sequence' name = 'x_fp_sequence' value = '' />
                    <input type = 'hidden' id = 'x_fp_timestamp' name = 'x_fp_timestamp' value = '' />


            <div id="m_divLineItems">
              <h3>Service Information</h3>
              <div id="ContentBodyPlaceholder_m_divTime"></div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblAccountNumber" class="InfoLabel">
                  Account Number</label>
                <input type = 'text' id = 'x_invoice_num' name = 'x_invoice_num' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblPhone" class="InfoLabel">
                  Phone</label>
                <input type = 'text' id = 'x_phone' name = 'x_phone' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblEMail" class="InfoLabel">
                  e-mail</label>
                <input type = 'text' id = 'x_email' name = 'x_email' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblServiceAddress" class="InfoLabel">
                  Service Address</label>
                <input type = 'text' id = 'x_ship_to_address' name = 'x_ship_to_address' value = '' />
              </div>
              <h3>Billing Information</h3>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblFirstName" class="InfoLabel">
                  First Name</label>
                <input type = 'text' id = 'x_first_name' name = 'x_first_name' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblLastName" class="InfoLabel">
                  Last Name</label>
                <input type = 'text' id = 'x_last_name' name = 'x_last_name' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblAddress" class="InfoLabel">
                  Address</label>
                <input type = 'text' id = 'x_address' name = 'x_address' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblCity" class="InfoLabel">
                  City</label>
                <input type = 'text' id = 'x_city' name = 'x_city' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblState" class="InfoLabel">
                  State</label>
                <input type = 'text' id = 'x_state' name = 'x_state' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblZip" class="InfoLabel">
                  Zip</label>
                <input type = 'text' id = 'x_zip' name = 'x_zip' value = '' />
              </div>
              <div class="LineItem">
                <label id="ContentBodyPlaceholder_m_lblAmount" class="InfoLabel">
                  Amount</label>&nbsp;
                <input type = 'text' id = 'x_amount' name = 'x_amount' value = '' />
                <input type="submit" class="submit" id="submitbutton" name="submitbutton" value="Pay Water Bill &gt;&gt;"/>
                </div>
    </div>





</form>


</body>
</html>

PHP Calc:

<?php

require_once (dirname(__FILE__).'./config.php');

$amount = $_POST['xamount'];

  $loginID      = AUTHORIZENET_API_LOGIN_ID;
  $transactionKey = AUTHORIZENET_TRANSACTION_KEY;


  // a sequence number is randomly generated
  $sequence = rand(1, 10000);
  // a timestamp is generated
  $timeStamp    = time();


  $x_fp_hash = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey);

  $x_fp_sequence = $sequence;

  $x_fp_timestamp = $timeStamp;


$result = json_encode(array("type"=>"success","fp_hash"=>"$x_fp_hash","fp_sequence"=>"$x_fp_sequence","fp_sequence"=>"$x_fp_sequence","fp_timestamp"=>"$x_fp_timestamp"));

echo $result;

//die();

?>