如何发布页面上所有点击的按钮?

时间:2011-04-28 08:08:17

标签: php post button

我有一个用户可以提问的页面。 当他们转到下一页时,我需要做一个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数组中,然后将数据提交到我的数据库?

3 个答案:

答案 0 :(得分:1)

考虑使用复选框?您也可以使用jQuery很好地设置样式。你真的不应该使用<button>

只需使用<input type="checkbox" name="question1">,然后使用jQueryUI buttons and checkboxes来设置样式。确保在检查后将其禁用,以便它不会被撤消。

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

感谢评论的反馈。

答案 1 :(得分:0)

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

至少在理论上你可以。版本8之前的Internet Explorer严重错误地处理了<button>标记。在Internet Explorer 6中,如果使用<button>标记,实际上基本上无法确定单击了哪个按钮。

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

答案 2 :(得分:0)

这是我的解决方案:

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