php 如何使用两个提交按钮,并区分使用哪个提交表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11929471/
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
How do I use two submit buttons, and differentiate between which one was used to submit the form?
提问by fvgs
Currently, I have an HTML form where the user will enter a title and text for an article. When it is time to submit, they are presented with two buttons. One is to 'save' their article without publishing it, and the other is to 'publish' the article and make it public.
目前,我有一个 HTML 表单,用户可以在其中输入文章的标题和文本。当需要提交时,他们会看到两个按钮。一种是“保存”他们的文章而不发表,另一种是“发表”文章并公开。
I'm using PHP, and I am trying to figure out how to tell which button was used, in order to store the appropriate corresponding value in the database.
我正在使用 PHP,我试图弄清楚如何判断使用了哪个按钮,以便在数据库中存储适当的相应值。
<td>
<input type="submit" class="noborder" id="save" value="" alt="Save" tabindex="4" />
</td>
<td>
<input type="submit" class="noborder" id="publish" value="" alt="Publish" tabindex="5" />
</td>
Probably should have mentioned this earlier, but I cannot assign the buttons values because the button is an image, so the text would show up above it.
可能应该早先提到这一点,但我无法分配按钮值,因为按钮是一个图像,所以文本会显示在它上面。
回答by powerbuoy
Give each inputa nameattribute. Only the clicked input's nameattribute will be sent to the server.
给每input一个name属性。只有 clickedinput的name属性会被发送到服务器。
<input type="submit" name="publish" value="Publish">
<input type="submit" name="save" value="Save">
And then
进而
<?php
if (isset($_POST['publish'])) {
# Publish-button was clicked
}
elseif (isset($_POST['save'])) {
# Save-button was clicked
}
?>
Edit: Changed valueattributes to alt. Not sure this is the best approach for image buttons though, any particular reason you don't want to use input[type=image]?
编辑:将value属性更改为alt. 不确定这是图像按钮的最佳方法,您不想使用的任何特殊原因input[type=image]?
Edit: Since this keeps getting upvotes I went ahead and changed the weird alt/valuecode to real submit inputs. I believe the original question asked for some sort of image buttons but there are so much better ways to achieve that nowadays instead of using input[type=image].
编辑:由于这不断获得赞成票,我继续将奇怪的alt/value代码更改为真正的提交输入。我相信最初的问题要求某种图像按钮,但现在有更好的方法来实现这一点,而不是使用input[type=image].
回答by Dr. Dan
Give name and values to those submit buttons like:
为这些提交按钮提供名称和值,例如:
<td>
<input type="submit" name='mybutton' class="noborder" id="save" value="save" alt="Save" tabindex="4" />
</td>
<td>
<input type="submit" name='mybutton' class="noborder" id="publish" value="publish" alt="Publish" tabindex="5" />
</td>
and then in your php script you could check
然后在你的 php 脚本中你可以检查
if($_POST['mybutton'] == 'save')
{
///do save processing
}
elseif($_POST['mybutton'] == 'publish')
{
///do publish processing here
}
回答by WatsMyName
If you can't put value on buttons. I have just a rough solution. Put a hidden field. And when one of the buttons are clicked before submitting, populate the value of hidden field with like say 1 when first button clicked and 2 if second one is clicked. and in submit page check for the value of this hidden field to determine which one is clicked.
如果你不能给按钮赋值。我只有一个粗略的解决方案。放置一个隐藏字段。当一个按钮在提交之前被点击时,当第一个按钮被点击时填充隐藏字段的值,例如 1 和 2 如果第二个按钮被点击。并在提交页面检查此隐藏字段的值以确定单击了哪个字段。
回答by Stranger
You can use it as follows,
您可以按如下方式使用它,
<td>
<input type="submit" name="save" class="noborder" id="save" value="Save" alt="Save"
tabindex="4" />
</td>
<td>
<input type="submit" name="publish" class="noborder" id="publish" value="Publish"
alt="Publish" tabindex="5" />
</td>
And in PHP,
而在 PHP 中,
<?php
if($_POST['save'])
{
//Save Code
}
else if($_POST['publish'])
{
//Publish Code
}
?>

