Php 多个提交按钮,如何确定单击了哪个按钮?

Php 多个提交按钮,如何确定单击了哪个按钮?,php,html,Php,Html,我有一个带有多个提交按钮的表单 每个提交按钮都是一个IMG SRC垃圾桶,表示基于web的邮件收件箱中邮件的删除图标 确定单击了哪个提交按钮图标的最佳方法是什么,这样我就可以编写PHP/MySQL代码来删除消息 if(!empty($_POST)){ // How do I figure out which submit button has been clicked to get the ID of the message to delete? } <form meth

我有一个带有多个提交按钮的表单

每个提交按钮都是一个
IMG SRC
垃圾桶,表示基于web的邮件收件箱中邮件的删除图标

确定单击了哪个提交按钮图标的最佳方法是什么,这样我就可以编写PHP/MySQL代码来删除消息

if(!empty($_POST)){
        // How do I figure out which submit button has been clicked to get the ID of the message to delete?
}

<form method="POST">
<input src="http://www.foo.com/img.png" id="button_1">
<input src="http://www.foo.com/img.png" id="button_2">
<input src="http://www.foo.com/img.png" id="button_3">
<input src="http://www.foo.com/img.png" id="button_4">
...
<input src="http://www.foo.com/img.png" id="button_100">
</form>
if(!empty($\u POST)){
//如何确定单击了哪个提交按钮以获取要删除的邮件的ID?
}
...

您可以为每个按钮指定
名称
。然后它将显示在
$\u POST['submit']

<img src="http://www.foo.com/img.png" id="button_4" name='submit' value='4' />

给每个按钮一个名称=“”

然后你可以做类似的事情

isset($_POST['button_name']) {
      // execute code here if true
}

为每个submit按钮设置
,并在php中检查该值,然后找到单击的按钮

<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>

...

echo$\u POST['submit\u btn']
将为您提供单击提交按钮的值

您必须通过清除每个按钮的名称和值将值传递给当前文件。。然后,您可以在php脚本中回显,以了解单击了哪个脚本。

此问题的解决方案是使用标记输入/按钮的NAME属性

<form method="POST">
<img src="http://www.foo.com/img.png" id="button_1" name="submit_btn" value="1">
<img src="http://www.foo.com/img.png" id="button_2" name="submit_btn" value="2">
<img src="http://www.foo.com/img.png" id="button_3" name="submit_btn" value="3">
<img src="http://www.foo.com/img.png" id="button_4" name="submit_btn" value="4">
...
<img src="http://www.foo.com/img.png" id="button_100" name="submit_btn" value="100">
</form>
<input type="submit" name="submitSave" value="Save"/>
<input type="submit" name="submitAddComment" value="Add comment"/>

这么多年以后,我喜欢
按钮
,因为它可以独立于返回的值显示文本或图像

这里是一个适合本帖标题和比OP更多案例的可能性说明

<?php
if(!empty($_POST['id'])){
  echo 'button '. $_POST['id'] .' clicked';
} elseif ('create' === ($_POST['action'] ?? '')) {
  echo 'create clicked'; // ?action=create
} elseif (isset($_POST['action'])) {
  echo 'refresh clicked'; // ?action
} elseif (isset($_POST)) {
  echo 'Default clicked'; // ?
}
?>
<form method="POST">

  <!-- Original Post examples -->
  <button type="submit" name="id" value="1"><img src="http://www.foo.com/img.png"></button>
  <button type="submit" name="id" value="2"><img src="http://www.foo.com/img.png"></button>
  ...
  <button type="submit" name="id" value="100"><img src="http://www.foo.com/img.png"></button>

  <!-- Additional possibilities -->
  <!-- ?action=create -->
  <button type="submit" name="action" value="create">New element</button>
  <!-- ?action -->
  <button type="submit" name="action">Refresh</button>
  <!-- ? -->
  <button type="submit">Default</button>
</form>

...
新元素
刷新
违约

oops对不起。。它应该读可能重复的可能重复的不认为这是一个重复,该链接是为2提交按钮,我正在寻找代码,为2,5,10或1000工程。。。因此,一个序列化的解决方案只需应用相同的技巧一千次。。。我看不出有什么问题;-)我不能那样做,因为可能有数百个按钮。。你如何解释他们所有人检查哪一个被点击?好的,谢谢。。那我怎么检查哪个被点击了?@bobafart你。。。不,停下来。你的名字。我只是。什么?User016,成功了。干得好。我不明白它为什么起作用。我本以为submit_btn会始终保留分配给它的最后一个值。。。但由于某种原因,它没有。不知道为什么。但是成功了!谢谢,这里有同样的问题:为什么会这样?什么是浏览器机制?为什么它不包含最后一个值?
<?php
if(!empty($_POST['id'])){
  echo 'button '. $_POST['id'] .' clicked';
} elseif ('create' === ($_POST['action'] ?? '')) {
  echo 'create clicked'; // ?action=create
} elseif (isset($_POST['action'])) {
  echo 'refresh clicked'; // ?action
} elseif (isset($_POST)) {
  echo 'Default clicked'; // ?
}
?>
<form method="POST">

  <!-- Original Post examples -->
  <button type="submit" name="id" value="1"><img src="http://www.foo.com/img.png"></button>
  <button type="submit" name="id" value="2"><img src="http://www.foo.com/img.png"></button>
  ...
  <button type="submit" name="id" value="100"><img src="http://www.foo.com/img.png"></button>

  <!-- Additional possibilities -->
  <!-- ?action=create -->
  <button type="submit" name="action" value="create">New element</button>
  <!-- ?action -->
  <button type="submit" name="action">Refresh</button>
  <!-- ? -->
  <button type="submit">Default</button>
</form>