Javascript 无法使用ajax表单提交功能自动发布
在这个网页中,我有一个带有提交按钮的可见表单,称为表单a。它有一个post操作Javascript 无法使用ajax表单提交功能自动发布,javascript,php,ajax,forms,html-post,Javascript,Php,Ajax,Forms,Html Post,在这个网页中,我有一个带有提交按钮的可见表单,称为表单a。它有一个post操作 这是一个示例代码(根据注释) 你有你的表格A <form name="A" id="myFormA" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <== first visable form ,Submitting the data into DB ........f
这是一个示例代码(根据注释)
你有你的表格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>
你在哪里提交
隐藏表单?@Yuri隐藏表单将发布到ePaymet网关?表单不是自行提交的,它需要有人手动或通过脚本提交
是submit
事件的处理程序。但是谁在触发那个事件呢?我明白了。那么js或Ajax的任何功能都可以执行自动提交功能吗?我应该现在用定义payform
id,payform
和form-A
都没有被触发。据我所知,在提交form-A时,你想要阻止它并提交另一个(隐藏的)名为PayForm的表单
-->我想您误解了它的含义。过程是:用户按form-A提交按钮-->发布表单A并存储在DB-->触发PayForm-->用户直接发送到另一个PayForm post URL简言之,我希望用户按form-A
并触发这两个表单。最后,用户将直接处理用户至支付表单
POST URLCheck编辑的答案。变量payFormDone
将在提交myFormA
//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>
<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>
<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>
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();
});
});