我有一个带有POST方法的表单和另一个页面的操作。
在表单中,我有另一个表单,我需要使用不同的操作提交,但它使用主表单操作提交。
这是我的第二种形式:
<script>
function formSubmit()
{
document.getElementById("invoices_form").submit();
}
</script>
<form action="resend_multiple_invoices.php" name="invoices_form" method="post">
<input type="button" onclick="formSubmit()" value="Send Invoices" />
</form>
我怎样才能让它提交第二份表格而不是主要表格?
您可以在输入字段中使用"form"属性,然后混合所有输入。通过提交,他们指的是正确的表格。
<form action="" method="post" id="form1"></form>
<form action="" method="post" id="form2"></form>
<input name="firstname" form="form1">
<input name="firstname" form="form2">
<button type="submit" name="submit" form="form1">Save form 1</button>
<button type="submit" name="submit" form="form2">Save form 2</button>
另请参阅 https://www.w3schools.com/tags/att_input_form.asp
您不能(通常(将嵌套表单与其父表单分开提交。嵌套表单是 W3C 禁令中概述的无效 HTML。
为了解决您的问题,我建议您使用以下两种单独的形式:
<script>
function invoicesFormSubmit()
{
document.getElementById("invoices_form").submit();
}
function otherFormSubmit()
{
document.getElementById("other_form").submit();
}
</script>
<form action="resend_multiple_invoices.php" name="invoices_form" method="post">
//
// Input fields go here
//
<input type="button" onclick="invoicesFormSubmit()" value="Send Invoices" />
</form>
<form action="other_method.php" name="other_form" method="post">
//
// Input fields go here
//
<input type="button" onclick="otherFormSubmit()" value="Other Method" />
</form>
JQuery.ajax 和 html 用于通过 ajax 验证"内部表单",然后提交整个表单。 我在这两种情况下都使用 ajax 来显示控制器.php文件和提交 ID 的用途。 你也可以有一个内部表单,它由几个隔离的部分组成,通过使用类而不是id作为Jquery选择器。
<form>
<input />
<textarea />
<select /> <!-- etc. -->
<section id="verify">
<input />
<textarea />
<select /> <!-- etc -->
<button type="button">submit</button>
<!-- eg. sub-submission verifies data in section -->
</section>
<select />
<input />
<input type="submit" value="submit" />
</form>
<script>
$(document).ready(function() {
$("#verify button").on ('click', verify);
$('form').submit (formSend);
function verify (){
// get input data within section only (ie. within inner form)
var postData = $('#verify').filter(':input' ).serializeArray();
postData.push ({name:"submitId", value:'verify'});
var request = $.ajax ({
type: "POST",
url: "controller.php",
data: postData,
error: function (xhr, status, message){
alert (status);
}
});
}
function formSend (){
// get input data within entire form
var postData = $(this).serializeArray();
postData.push ({name:"submitId", value:'send'});
var request = $.ajax ({
type: "POST",
url: "controller.php",
data: postData,
error: function (xhr, status, message){
alert (status);
}
});
}
});
</script>