在另一个表格中提交表格

use*_*234 2 javascript php forms

我有一个使用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>
Run Code Online (Sandbox Code Playgroud)

我怎么能让它提交第二个表格而不是主要表格?

Mr *_*nes 9

您不能(普遍)提交与其父表单分开的嵌套表单.嵌套表单是无效的HTML,如W3C禁令中所述.

为了解决您的问题,我建议您使用以下两种形式:

<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>
Run Code Online (Sandbox Code Playgroud)


小智 5

您可以在输入字段中使用“表单”属性,然后混合所有输入。通过提交,他们指的是正确的表格。

<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>
Run Code Online (Sandbox Code Playgroud)

另见https://www.w3schools.com/tags/att_input_form.asp