如何使用两个提交按钮,并区分用于提交表单的按钮?(复制)

时间:2022-09-25 15:00:39

This question already has an answer here:

这个问题已经有了答案:

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.

可能在前面已经提到过,但是我不能分配按钮的值,因为按钮是一个图像,所以文本会显示在它上面。

4 个解决方案

#1


54  

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

给每个输入一个name属性。只有单击的输入的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 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]吗?

Edit: 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].

编辑:因为这一直在获得向上的投票,所以我把奇怪的alt/value代码改为真正的提交输入。我相信最初的问题是关于图像按钮的,但是现在有很多更好的方法来实现它,而不是使用input[type=image]。

#2


7  

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
}

#3


2  

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。在提交页面中检查隐藏字段的值,以确定单击哪个字段。

#4


1  

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
}
?>

#1


54  

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

给每个输入一个name属性。只有单击的输入的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 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]吗?

Edit: 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].

编辑:因为这一直在获得向上的投票,所以我把奇怪的alt/value代码改为真正的提交输入。我相信最初的问题是关于图像按钮的,但是现在有很多更好的方法来实现它,而不是使用input[type=image]。

#2


7  

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
}

#3


2  

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。在提交页面中检查隐藏字段的值,以确定单击哪个字段。

#4


1  

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
}
?>