我如何使用两个提交按钮,并区分使用哪个提交表单? [英] How do I use two submit buttons, and differentiate between which one was used to submit the form?

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

问题描述

目前,我有一个 HTML 表单,用户可以在其中输入文章的标题和文本.当需要提交时,他们会看到两个按钮.一种是保存"他们的文章而不发表,另一种是发表"文章并公开.

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.

我正在使用 PHP,我想弄清楚如何判断使用了哪个按钮,以便在数据库中存储适当的对应值.

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.

<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.

推荐答案

给每个 input 一个 name 属性.只有被点击的inputname属性会被发送到服务器.

Give each input a name attribute. Only the clicked input's name attribute will be sent to the server.

<input type="submit" name="publish" value="Publish">
<input type="submit" name="save" value="Save">

然后

<?php
    if (isset($_POST['publish'])) {
        # Publish-button was clicked
    }
    elseif (isset($_POST['save'])) {
        # Save-button was clicked
    }
?>

Edit:将 value 属性更改为 alt.不确定这是图像按钮的最佳方法,您不想使用 input[type=image] 的任何特殊原因?

Changed value attributes 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]?

由于这不断获得赞成票,我继续将奇怪的 alt/value 代码更改为真正的提交输入.我相信最初的问题要求某种图像按钮,但现在有更好的方法来实现这一点,而不是使用 input[type=image].

Since this keeps getting upvotes I went ahead and changed the weird alt/value code 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].

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

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