【问题标题】:How can I POST all the clicked buttons on a page?如何发布页面上所有点击的按钮?
【发布时间】:2011-04-28 08:08:17
【问题描述】:

我有一个用户可以在其中提问的页面。 当他们进入下一页时,我需要进行 mysql 查询并将所有提出的问题发布到数据库中。我使用 jquery 隐藏答案并在单击相应按钮时显示它们。

页面结构如下:

<!-- questions.php -->

<form action="submitquestions.php" method="post">
   <div>
      <span>This is a question</span>
      <button name="question" value="question1">Ask the question</button>
      <span>This is the answer</span>
   </div>
   <div>
      <span>This is a question</span>
      <button name="question" value="question2">Ask the question</button>
      <span>This is the answer</span>
   </div>
   <div>
   ....
   </div>
   <button name="submit">Go to the next page</button>
</form>

<!-- submitquestions.php -->
<?php if (isset($_POST['submit'])) {

        var_dump($_POST);

} ?>

如何将所有点击的按钮放入 $_POST 数组,然后将数据提交到我的数据库?

【问题讨论】:

  • 你需要收集每个问题的答案吗?如果有,你是如何收集的?

标签: php post button


【解决方案1】:

考虑使用复选框?您也可以使用 jQuery 很好地设置它们的样式。无论如何,你真的不应该使用&lt;button&gt;s。

只需使用&lt;input type="checkbox" name="question1"&gt; 然后jQueryUI buttons and checkboxes 来设置样式。确保在检查后将其禁用,以免被撤消。

然后在 PHP 中,检查 if(isset($_POST['question1'] )) { 以查看是否已选中某个框。

感谢 cmets 的反馈。

【讨论】:

  • 关于$_POST['question1'] == 'on' - 我会使用isset($_POST['question1'])。
  • 如果一个按钮被多次点击,这个解决方案就会出现问题。在第一次单击后使用一些 js 禁用它们可能是一种解决方法。我也同意 binaryLV 关于使用 isset,比检查它的值是否为 'on' 更好的解决方案
  • 很好的解决方案 :) 那也可以!感谢邓纳姆的帮助
【解决方案2】:

这是我的解决方案:

<form action="submitquestions.php" method="post">
   <div>
      <span>This is a question</span>
      <input type="checkbox" name="question[]" value="question1" style="display:none">
      <button>Ask the question</button>
      <span>This is the answer</span>
   </div>
   <div>
      <span>This is a question</span>
      <input type="checkbox" name="question[]" value="question2" style="display:none">
      <button>Ask the question</button>
      <span>This is the answer</span>
   </div>
   <div>
   ....
   </div>
   <button name="submit">Go to the next page</button>
</form>
<script>
  $(document).ready(function(){
    $("form button").click(function(){
       $(this).prev(":checkbox").attr("checked", true);
    });
  });
</script>

<!-- submitquestions.php -->
<?php if (isset($_POST['submit'])) {
    if(!isset($_POST['question'])) $_POST['question'] = array(); //Just in the case no button is pressed
    //$_POST['question'] will have an array of all clicked questions.
    var_dump($_POST);

} ?>

如您所见,我们在每个按钮的“后面”使用“隐藏”复选框,用于检查按钮何时被按下。而且,由于我们的按钮没有名称但复选框有名称,因此我们只将选中的复选框发送到服务器。

$_POST['question'] 将包含所有已点击问题的数组。每个问题都不需要“isset”

【讨论】:

  • 关于“每个问题都不需要 'isset'”部分 - 如果用户不选中任何复选框怎么办?
  • @binaryLV 不需要每个问题的 isset,而是每个数组的 isset 以防万一。我将对其进行编辑以使其更清晰,谢谢
【解决方案3】:

如果您使用&lt;button type="submit" name="this button's unique name goes here" value="1"&gt;Button text goes here&lt;/button&gt;,那么您可以使用$_POST ['this button's unique name goes here'] == 1检查触发提交的按钮

至少理论上可以。版本 8 之前的 Internet Explorer 对 &lt;button&gt; 标记的处理非常糟糕。在 Internet Explorer 6 中,如果您使用 &lt;button&gt; 标签,实际上基本上不可能确定单击了哪个按钮。

http://www.w3schools.com/tags/tag_button.asp

【讨论】:

    猜你喜欢
    • 2014-09-10
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2020-01-11
    • 2016-08-02
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多