如何使用两个提交按钮,并区分哪一个用于提交表单?

问题描述:

目前,我有一个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.

给每个输入一个名字属性。只有点击输入名称属性才会被发送到服务器。

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

然后

And then

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

编辑:已更改属性到 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 / 代码到真正的提交输入。我相信最初的问题需要某种图像按钮,但现在有更好的方法来实现,而不是使用 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].