简体   繁体   English

它无法使用ajax表单提交功能自动发布

[英]It is not able to auto-post with ajax form submit function

In this webpage , I have a visible form with a submit button ,called form A.It has a post action. 在此网页中,我有一个带有提交按钮的可见表单,称为表单A。它具有发布操作。

<form name="payFormCcard" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">

I want to do an invisible form that extract some of the data from form-A , with the method of hidden input button .It will automatically execute the second form called payForm and post to the another place with the JS. 我想做一个不可见的表单,使用隐藏输入按钮的方法从表单A提取一些数据,它将自动执行第二个表单payForm并使用JS发布到另一个地方。

Now , it only execute the form-A and post to the DB. 现在,它仅执行Form-A并将其发布到DB。

It cannot do the second form - payForm auto posting . 它无法执行第二个表单payForm自动过帐。

~~Remark , I didn't add the submit button for payForm because I want to execute the second invisible form automatically after filling the first form . ~~备注,我没有为payForm添加提交按钮,因为我想在填写第一个表单后自动执行第二个不可见表单。

Here is my code: 这是我的代码:

<form name="A"  action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">  <== first visable form ,Submitting the data into DB
    ........field inputs. ..... 
    <input type="submit" class="btn btn-primary" value="Submit">
</form>

 //invisible table
<form name="payForm" method="post" action=" https://test.paydollar.com/b2cDemo/eng/payment/payForm.jsp">
    <input type="hidden" name="merchantId" value="sth">
    <input type="hidden" name="amount" value="</?php echo $input_amount; ?>" >
    <input type="hidden" name="orderRef" value="<?php  date_default_timezone_set("Asia/Taipei");  $date = date('m/d/Y h:i:s a', time()); echo $date ; ?>">
    <input type="hidden" id="currCode" value="sth" >
    <input type="hidden" id="mpsMode" value="sth" >
    <input type="hidden" id="successUrl" value="http://www.yourdomain.com/Success.html">
    <input type="hidden" id="failUrl" value="http://www.yourdomain.com/Fail.html">
    <input type="hidden" id="cancelUrl" value="http://www.yourdomain.com/Cancel.html">
    ...
</form>

<script type='text/javascript'>
/* attach a submit handler to the form */
$("#payForm").submit(function(event) {
    /* stop form from submitting normally */
    event.preventDefault();

    /* get the action attribute from the <form action=""> element */
    var $form = $( this ),
    url = $form.attr( 'action' );

    /* Send the data using post with element id name and name2*/
    var posting = $.post( url, { merchantId: $('#merchantId').val(), amount: $('#amount').val(), orderRef: $('#orderRef').val() , currCode: $('#currCode').val() , mpsMode: $('#mpsMode').val(), successUrl: $('#successUrl').val(), failUrl: $('#failUrl').val(), cancelUrl: $('#cancelUrl').val(), payType: $('#payType').val(), lang: $('#lang').val(), payMethod: $('#payMethod').val(), secureHash: $('#secureHash').val()} );

    /* Alerts the results */
    posting.done(function( data ) {
        alert('success');
        else('gg');
    });
});
</script>

This is a sample code (according to comments). 这是一个示例代码(根据注释)。

You have your form-A 你有你的表格-A

<form name="A" id="myFormA" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">  <== first visable form ,Submitting the data into DB
    ........field inputs. ..... 
    <input type="submit" class="btn btn-primary" value="Submit">
</form>

From what I understood, on submit of form-A, you want to prevent it and submit another (hidden) form named PayForm 据我了解,在提交A表格时,您想阻止它并提交另一个名为PayForm的(隐藏)表格

<form name="payForm" id="payForm" method="post" action=" https://test.paydollar.com/b2cDemo/eng/payment/payForm.jsp">
    <input type="hidden" name="merchantId" value="sth">
    <input type="hidden" name="amount" value="<?php echo $input_amount; ?>" >
    <input type="hidden" name="orderRef" value="<?php  date_default_timezone_set("Asia/Taipei");  $date = date('m/d/Y h:i:s a', time()); echo $date ; ?>">
    <input type="hidden" id="currCode" value="sth" >
    <input type="hidden" id="mpsMode" value="sth" >
    <input type="hidden" id="successUrl" value="http://www.yourdomain.com/Success.html">
    <input type="hidden" id="failUrl" value="http://www.yourdomain.com/Fail.html">
    <input type="hidden" id="cancelUrl" value="http://www.yourdomain.com/Cancel.html">
    ...
</form>

You can do this with the following script: 您可以使用以下脚本执行此操作:

var payFormDone = false;
$('#myFormA').on('submit', function(e){
    if( !payFormDone ) {
        e.preventDefault(); // THIS WILL TRIGGER THE NEXT CODE
        $('#payForm').submit();
    }
});

$("#payForm").submit(function(event) {
    /* stop form from submitting normally */
    event.preventDefault();

    /* get the action attribute from the <form action=""> element */
    var $form = $( this ),
    url = $form.attr( 'action' );

    /* Send the data using post with element id name and name2*/
    var posting = $.post( url, { 
            merchantId: $('#merchantId').val(), 
            amount: $('#amount').val(), 
            orderRef: $('#orderRef').val(), 
            currCode: $('#currCode').val(), 
            mpsMode: $('#mpsMode').val(), 
            successUrl: $('#successUrl').val(), 
            failUrl: $('#failUrl').val(), 
            cancelUrl: $('#cancelUrl').val(), 
            payType: $('#payType').val(), 
            lang: $('#lang').val(), 
            payMethod: $('#payMethod').val(), 
            secureHash: $('#secureHash').val()
    } );

    /* Alerts the results */
    posting.done(function( data ) {
        alert('success');
        payFormDone = true;
        $('#myFormA').submit();
    });
});

NOTE: you were using $('#payForm') but the ID attribute was not defined on the form.. 注意:您正在使用$('#payForm')但未在表单上定义ID属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM