[英]Submit same form with three buttons using php
I am having a problem to submit same form three time but with different actions. 我有一个问题是提交相同的表格三次,但采取不同的行动。 Please see the following code:
请参阅以下代码:
if(isset($_POST['btn_submit'])){
//Some code here
}
if(isset($_POST['btn_sub'])){
// Some other code here
}
my html code is as follows: 我的HTML代码如下:
<form name="form" id="form" action="" method="" enctype="multipart/form-data">
<table>
<tr>
<!-- Some input fields -->
<input type="submit" name="btn_submit" id="btn_submit" value="Submit" />
</tr>
<tr>
<!-- Some more input fields -->
<input type="submit" name="btn_sub" id="btn_sub" value="Submit" />
</tr>
</table>
</form>
Now when I clicked on first submit button is clicked then I got the posted values in POST array (with button name ie. $_POST['btn_submit']=> Submit
) but when clicked on second button then also got all posted values in POST array except name of the button (ie. $_POST['btn_sub'] => Submit
). 现在,当我点击第一个提交按钮时,我点击POST数组中的已发布值(带按钮名称即
$_POST['btn_submit']=> Submit
。 $_POST['btn_submit']=> Submit
POST $_POST['btn_submit']=> Submit
)但是当点击第二个按钮时,也会在POST中获得所有发布的值数组除了按钮的名称(即$_POST['btn_sub'] => Submit
)。 On second button click I want to update some database records but because of its condition if(isset($_POST['btn_sub']))
not true so it's not enter into this condition and my database records are not updated, rest of the array elements shows but not the submit button. 在第二个按钮单击我想更新一些数据库记录,但由于其条件
if(isset($_POST['btn_sub']))
不正确所以它不进入这种情况,我的数据库记录没有更新,其余的数组元素显示但不是提交按钮。
This functionality is work on my local server but after uploading it on live server its not working. 此功能在我的本地服务器上运行,但在将其上传到实时服务器后,它无法正常工作。
I don't understand why this is happened, please help me for this. 我不明白为什么会这样,请帮帮我。
Try this code 试试这个代码
<form name="form" id="form" action="" method="POST" enctype="multipart/form-data">
<table>
<tr>
<!-- Some input fields -->
<input type="submit" name="btn_submit" id="btn_submit" value="Submit2" />
</tr>
<tr>
<!-- Some more input fields -->
<input type="submit" name="btn_sub" id="btn_sub" value="Submit1" />
</tr>
</table>
</form>
Use same name for submit button 使用相同的名称提交按钮
<form name="form" id="form" action="" method="POST" enctype="multipart/form-data">
<table>
<tr>
<!-- Common input fields -->
<input type="text" name="commontxt" id="commontxt" value="common text field" />
</tr>
<tr>
<!-- Some input fields -->
<input type="submit" name="btn_submit" id="btn_submit1" value="Submit 1" />
</tr>
<tr>
<!-- Some more input fields -->
<input type="submit" name="btn_submit" id="btn_submit2" value="Submit 2" />
</tr>
</table>
</form>
Check which submit button is click by this way 通过这种方式检查单击哪个提交按钮
if ( !empty( $_POST ) )
{
if ($_POST['btn_submit'] == 'Submit 1') {
//action for Submit 1 here
$commontxt = $_POST['commontxt'];
echo 'Submit 1<br/>';
echo $commontxt;
} else if ($_POST['btn_submit'] == 'Submit 2') {
//action for Submit 2 here
$commontxt = $_POST['commontxt'];
echo 'Submit 2<br/>';
echo $commontxt;
}
}
There are many approaches you can follow here:: 您可以在此处遵循许多方法::
Approach 1: 方法1:
If you can change the page on submit, means on click of submit request goes to two different pages. 如果您可以在提交时更改页面,则单击提交请求的方式将转到两个不同的页面。 There is new HTML5 approach to handle this:
有新的HTML5方法来处理这个问题:
<button type="submit" formaction="/action_one">SUBMIT</button>
<button type="submit" formaction="/action_two">SUBMIT</button>
Apparently this does not work in IE9 and earlier, but for other browsers you should be fine (see: w3schools.com HTML formaction Attribute). 显然这在IE9及更早版本中不起作用,但对于其他浏览器,你应该没问题(参见: w3schools.com HTML格式属性)。
Approach 2 方法2
Javascript Method: Javascript方法:
<input type="submit" value="dosomething" onclick="javascript:
form.action='actionurl1';"/>
<input type="submit" value="dosomethingelse" onclick="javascript:
form.action='actionurl2';"/>
Approach 3 方法3
If you want to keep same page on submit: 如果您想在提交时保持同一页面:
<input type="submit" name="row[1]" value="Submit">
<input type="submit" name="row[2]" value="Submit">
And then in the server side (PHP in my example) you can read "row" as an array to get the index: 然后在服务器端(在我的示例中为PHP),您可以将“row”作为数组读取以获取索引:
$index = key($_POST['row']);
$_POST['row'] will be an array with just one element, in the form index => value (for example: '2' => 'something"). $ _POST ['row']将是一个只包含一个元素的数组,格式为index => value(例如:'2'=>'something“)。
Read Here About Index Here 在这里阅读索引
=========================== New Answer as per OPs approach ================== =========================== 根据OP方法的新答案 ================= =
I have tried your Form Structure and i am getting second button name perfectly. 我已经尝试了你的表单结构,我完全得到了第二个按钮名称。
<?php
if(isset($_POST['btn_submit']) && empty($_POST['btn_sub']) ){
echo "First Submit-----------<br/>";
print_r($_POST);
}
else if(isset($_POST['btn_sub']) && empty($_POST['btn_submit'])){
echo "Second Submit-----------<br/>";
print_r($_POST);
}
?> ?>
HTML HTML
<form name="form" id="form" action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<input type="text" value="Form1" name="form1_input" />
<input type="submit" name="btn_submit" id="btn_submit" value="Submit" />
</tr>
<tr>
<input type="text" value="Form2" name="form2_input" />
<input type="submit" name="btn_sub" id="btn_sub" value="Submit" />
</tr>
</table>
</form>
Check this out and also confirm if you are nor repeating the same name on the page...
检查一下,并确认您是否在页面上重复相同的名称...
My output on second submit:: 我的第二次提交输出::
Second Submit-----------
Array ( [form1_input] => Form1 [form2_input] => Form2 [btn_sub] => Submit )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.