Reputation: 45
I'm making online test/quiz for students and using PHP and MySQL. Currently I have a form, where I can add the questions and answers into the database.
However, I have trouble with submitting correct answers into the database by selecting multiple checkboxes. I want to store all correct answers in the table called correct_answer(id, question_id, answer_id) and insert all selected id's from the table called answers(id,answer_id, answer).
How can I insert only selected checkboxes values and skip somehow unchecked checkboxes?
My HTML part:
<form id="add_question_form" method="post">
<label>Question: *</label>
<input id="question" type="text" name="question" class="form_default" placeholder="Type your question here" required>
<div class="inputs">
<label>Answer #1: *</label>
<input type="text" id="answer" name="dynamic[]" class="form_default" placeholder="answer" required/>
<input type="checkbox" name="selected_item[]" id="checkbox" value="<?php echo $answer_id;?>">
<label>correct</label>
<br>
<label>Answer #2: *</label>
<input type="text" id="answer" name="dynamic[]" class="form_default" placeholder="answer" required/>
<input type="checkbox" name="selected_item[]" id="checkbox" value="<?php echo $answer_id;?>" />
<label>correct</label>
<br>
</div>
<input id="submit" type="submit" name="add_question" value="Submit" class="button">
</form>
PHP code:
if (isset($_POST['add_question'])) { // Checks if the form has been submitted
if (isset($_POST['selected_item']) && !empty($_POST['selected_item'])) {
$counter++; // each time adds 1 after user posts the question
$optionArray = $_POST['dynamic']; // submits multiple answers to the database
for ($j = 0; $j < count($optionArray); $j++) {
$answer = mysql_real_escape_string($_POST['dynamic'][$j]); // returns multiple answers
$check_answer = "SELECT id, answer FROM answers WHERE answer = '$answer'";
$check_answer = mysql_query($check_answer); //checks if answer is exist in the database
while ($row = mysql_fetch_assoc($check_answer)) { // fetch a result rows as an associative array
$answer_id = $row['id']; // answer id from database
}
if (mysql_num_rows($check_answer) > 0) {
$q = "INSERT INTO correct_answer (question_id, answer_id) VALUES ('$counter','$answer_id')";
$q = mysql_query($q);
}
}
}
}
For instance:
I'm adding question and three answers "a","b" and "c". Selecting "a" and "c" as correct answer.
All answers have been successfully added to MySQL "answers" table:
1 - a
2 - b
3 - c
Can anyone help me please? I want to insert multiple correct answers, but only if they are selected..
Thanks in advance, any help will be appreciated!
Upvotes: 1
Views: 6456
Reputation: 5991
You can use an IF statement
if the checkbox is checked or not.
for ($j = 0; $j < count($_POST['selected_item']); $j++) {
if(!empty($_POST['selected_item'][$j])){ /* CHECK IF CHECKBOX IS SELECTED */
answer = mysql_real_escape_string($_POST['dynamic'][$j]); // returns multiple answers
$check_answer = "SELECT id, answer FROM answers WHERE answer = '$answer'";
$check_answer = mysql_query($check_answer); //checks if answer is exist in the database
while ($row = mysql_fetch_assoc($check_answer)) { // fetch a result rows as an associative array
$answer_id = $row['id']; // answer id from database
}
if (mysql_num_rows($check_answer) > 0) {
$q = "INSERT INTO correct_answer (question_id, answer_id) VALUES ('$counter','$answer_id')";
$q = mysql_query($q);
}
} /* END OF IF $answer is not empty */
} /* END OF FOR LOOP */
Use a counter
in your form and put it inside the array before submitting it. Example:
$counter = 0;
<input type="text" id="answer" name="dynamic[<?php $counter; ?>]" class="form_default" placeholder="answer" value="<?= isset($_POST['dynamic'][0]) ? $_POST['dynamic'][0] : '' ?>" required/>
<input type="checkbox" name="selected_item[<?php echo $counter; ?>]" id="chechbox" value="<?php echo $answer_id;?>">
Then increment it (assuming that you also run the form in a loop)
$counter = $counter + 1;
It doesn't run on a loop and the form is fix:
You can manually put the array number inside the textbox and checkbox.
After submission:
And when submitted, count the submitted checkbox using count()
function of PHP.
$counter = count($_POST["selected_item"]);
Then run your loop using the $counter
(refer back to my first given sample code).
A much more simple example:
Answer # 1
<input type="checkbox" name="selected_item[0]"><input type="text" name="dynamic[0]"> <!-- Notice that they are both in 0 array -->
Answer # 2
<input type="checkbox" name="selected_item[1]"><input type="text" name="dynamic[1]"> <!-- Next question array should be incremented from the previous answer -->
And after submission, count the selected_item
and then loop it that checks the selected checkboxes.
for($x=0; $x<=count($_POST['selected_item']); $x++){
if(!empty($_POST['selected_item'][$x])){
/**** PLACE HERE YOUR INSERT QUERY ****/
}
}
Upvotes: 1