使用表单外的按钮提交表单 [英] Submit form with a button outside the form

查看:100
本文介绍了使用表单外的按钮提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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> 


推荐答案

POST数组中的值将会是所张贴元素的名称。我不是100%确定的,但我不认为表单名称将在$ _POST数组中。

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.

您可以使用

You can check for a post by using

if (!empty($_POST))
{

}

或您可以根据FORM

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

这篇关于使用表单外的按钮提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆