【问题标题】:Multiple Submit buttons, how do determine which one was clicked?多个提交按钮,如何确定点击了哪一个?
【发布时间】:2013-07-31 11:10:37
【问题描述】:

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

每个提交按钮都是一个 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>

【问题讨论】:

标签: php html


【解决方案1】:

这么多年后,我喜欢button,因为它允许独立于返回的值显示文本或图像。

这是一个符合这篇文章标题的可能性说明,并且比 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>

【讨论】:

    【解决方案2】:

    这个问题的解决方法是使用标签输入/按钮的NAME属性。

    <input type="submit" name="submitSave" value="Save"/>
    <input type="submit" name="submitAddComment" value="Add comment"/>
    

    <button type="submit" name="submitSave">Save</button>
    <button type="submit" name="submitAddComment">Add comment</button>
    

    我认为你也可以使用按钮标签的 value 属性,这绝对不可能使用 input 标签。

    如果您需要使用 ID 或其他变量,请使用 name="submitDelete[888]" 然后,用 PHP 检查它:

    if( isset($_POST['submitDelete']) ) {
        echo key($_POST['submitDelete']);// Displays the ID to delete, e.g. 888.
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        为每个提交按钮设置value,并在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 $_POST['submit_btn']; 会给你点击哪个提交按钮的值

        【讨论】:

        • 好的,谢谢.. 那我如何查看点击了哪个?
        • @bobafart 你......只是没有。停止。你的名字。我只是。什么?
        • User016,它成功了。不错的工作。我不明白它为什么起作用。我会认为 submit_btn 将始终保留分配给它的最后一个值......但由于某种原因它没有。不知道为什么。但它奏效了!谢谢
        • 是的,同样的问题:为什么这样有效?什么是浏览器机制?为什么它不包含最后一个值?
        【解决方案5】:

        给每个按钮一个名字=""

        然后你可以做类似的事情

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

        【讨论】:

        • 我不能这样做,因为可能有数百个按钮..您如何考虑所有这些按钮以检查哪个按钮被点击?
        【解决方案6】:

        您可以为每个按钮指定namevalue。然后它将显示在$_POST['submit']

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

        【讨论】:

          猜你喜欢
          相关资源
          最近更新 更多
          热门标签