How can I POST all the clicked buttons on a page?
I have a page in where users can ask questions. When they move on to the next page, I need to do a mysql query and post all the asked question into the database. I use jquery to hide the answers and show them when clicking the corresponding button.
The structure of the page looks like this:
<!-- 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);
} ?>
How can I put all the clicked buttons into the $_POST array and that way submit the data to my database?
Considered using checkboxes? You could style them nicely with jQuery as well. You really shouldn't use <button>
s anyway.
Simply use <input type="checkbox" name="question1">
then the jQueryUI buttons and checkboxes to style it. Make sure your disable it once it has been checked so it doesn't get undone.
Then in PHP, check if(isset($_POST['question1'] )) {
to see if a box has been checked.
Thanks for feedback from comments.
Here's my solution:
<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);
} ?>
As you can see, we use 'hidden' checkboxes 'behind' each button, that check when a button is pressed. And, as our buttons don't have name but checkboxes do, we send only the checked checkboxes to the server.
$_POST['question']
will have an array of all clicked questions. No 'isset' per question needed
If you use <button type="submit" name="this button's unique name goes here" value="1">Button text goes here</button>
then you can check for the button that triggered the submit with $_POST ['this button's unique name goes here'] == 1
At least in theory you can. Internet Explorer prior to version 8 mishandles the <button>
tag quite badly. In internet explorer 6 it is in fact basically impossible to determine which button was clicked if you use the <button>
tag.
http://www.w3schools.com/tags/tag_button.asp
精彩评论