javascript 使用表单外的按钮提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19435670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:35:23  来源:igfitidea点击:

Submit form with a button outside the form

javascriptphphtml

提问by EM10

I have a form which I use a button outside the form to submit. Below is the code on how I submit the form with a button from outside the form. The code below should echo something on the page. But I don't know why my text wont echo.

我有一个表单,我使用表单外的按钮提交。下面是关于我如何使用表单外部的按钮提交表单的代码。下面的代码应该在页面上回显一些内容。但我不知道为什么我的文字不会回显。

    <?php
    if(isset($_POST['partForm'])){
        echo "Test1";
    }
    if(isset($_POST['submitForm'])){
        echo "Test2";
    }
?>

<html>
<body>

<div>
<h2>Part</h2>
<form method="post" enctype="multipart/form-data" name="partForm" id="partForm" action="submit_form_button_outside.php">
SKU#: <input type="text" name="sku"><br>
Part: <select name="categories">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select><br>

Description: <input type="text" name="part"><br>
Quantity: <input type="text" name="part"><br>
Price: <input type="text" name="part"><br>
Select images: <input type="file" name="img" multiple><input type="submit" value="Upload">
</form>
</div>

<div>
    <button type="submit" form="partForm" value="submitForm">Add Item</button>
    <!--<button type="button" onclick="document.getElementById('partForm').submit();">Add Item</button>-->
    <!--<button type="button" onclick="document.forms['partForm'].submit(); return false">Add Item</button> -->
</div>

</body>
</html> 

回答by Cristian Bitoi

You could use the jQuery submit function like:

您可以使用 jQuery 提交功能,如:

$('#buttonId').click(function() {
    $('#partForm').submit();
});

Basically, when the outside button is clicked, the form is submitted.

基本上,当外部按钮被点击时,表单被提交。

回答by JTFRage

Your button is outside the form tag.

您的按钮位于表单标签之外。

If you want your button outside the form tags consider the following example:

如果您希望您的按钮位于表单标签之外,请考虑以下示例:

<form action="demo_form.php" method="get" id="form1">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
</form>

<button type="submit" form="form1" value="Submit">Submit</button>

Note the form attribute on the button.

注意按钮上的表单属性。

Next thing to check for is if the posted variables are set:

接下来要检查的是是否设置了发布的变量:

print_r($_POST);

回答by JRam

The values that will be in the POST array will be the names of the posted elements. I'm not 100% sure, but I don't think the form name will be in the $_POST array.

POST 数组中的值将是已发布元素的名称。我不是 100% 确定,但我认为表单名称不会出现在 $_POST 数组中。

You can check for a post by using

您可以使用以下方法检查帖子

if (!empty($_POST))
{

}

or you can check for individual values like $_POST['sku'] based on the input names within the FORM

或者您可以根据 FORM 中的输入名称检查诸如 $_POST['sku'] 之类的单个值

回答by Leo Bali

your code is working well , and the form is submitting , but you are testing a wrong values in $_POST['partForm'] , try to fill the value sku ,, and in ur php test $_POST['sku']

您的代码运行良好,表单正在提交,但是您在 $_POST['partForm'] 中测试了错误的值,尝试填充值 sku ,并在您的 php 测试中 $_POST['sku']

回答by Jelle Ferwerda

If you have a button outside the form, you wil lnot be able to submit the form with that button without using javascript.

如果您在表单外有一个按钮,您将无法在不使用 javascript 的情况下提交带有该按钮的表单。

The code I see lower down does look about right: Add Item

我在下方看到的代码看起来确实正确:添加项目

I assume that did not work. Did you veriufy that the form did submit? I think the form name is not always passed in the $_POST array. Better test for one of the elements, or just print_r($_POST); to show the submitted info.

我认为那没有用。您是否已验证该表单确实已提交?我认为表单名称并不总是在 $_POST 数组中传递。更好地测试其中一个元素,或者只是 print_r($_POST); 显示提交的信息。