2011-04-28 64 views
0

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

+0

你需要收集每个问题的答案吗?如果是这样,你怎么收集它? – ljkyser 2011-04-28 08:12:32

回答

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'

+0

关于“*否'isset'每个问题需要*”部分 - 如果用户没有检查任何复选框会怎么样? – binaryLV 2011-04-28 11:28:46

+0

伟大的解决方案埃德加,非常感谢你! – 2011-05-02 07:37:27

+0

不客气。来自玻利维亚拉巴斯的欢呼声 – 2011-05-02 07:52:06

1

考虑使用复选框?你也可以用jQuery很好地设计它们。无论如何,你真的不应该使用<button>

只需使用<input type="checkbox" name="question1">然后jQueryUI buttons and checkboxes来设置它。确保一旦它被选中就禁用它,这样它就不会被撤消。

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

感谢您对评论的反馈。

+1

关于'$ _POST ['question1'] =='on'' - 我会用'isset($ _ POST ['question1'])''。 – binaryLV 2011-04-28 08:27:51

+0

如果不止一次点击一个按钮,该解决方案将成为问题。在第一次点击之后使用一些js来禁用它们可能是解决方法。我也同意binaryLV关于使用isset,比检查它的值是'on' – Sondre 2011-04-28 08:50:30

+0

好的解决方案:)方法更好的解决方案:),这将工作太多! 感谢您的帮助dunham – 2011-05-02 07:40:48

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