xxreenaxx1 Posted February 27, 2011 Share Posted February 27, 2011 How do I post the values that are in a while loop and add them to mysql table? <form action="Test_Completed.php" method="post"> include '../Database/take_an_exam.php'; $intNumber = 1; while($info = mysql_fetch_array( $sql )) { echo "$intNumber, {$info['Que_Question']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice1[]\" value=\"{$info['Que_Choice1']}\" /> "; echo "{$info['Que_Choice1']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice2[]\" value=\"{$info['Que_Choice2']}\" /> "; echo "{$info['Que_Choice2']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice3[]\" value=\"{$info['Que_Choice3']}\" /> "; echo "{$info['Que_Choice3']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice4[]\" value=\"{$info['Que_Choice4']}\" /> "; echo "{$info['Que_Choice4']} <br />\n"; $intNumber++; } ?> <input type="submit" value="submit"/> </body> </html> </body> </html> Link to comment https://forums.phpfreaks.com/topic/228973-checkbox-in-a-while-loop/ Share on other sites More sharing options...
RussellReal Posted February 27, 2011 Share Posted February 27, 2011 #1 your question is very vague, #2 mysql_fetch_array is more or less for looping numerically through a resultset, you're using mysql_fetch_array the way you would use mysql_fetch_assoc, you're pulling duplicate entries to the array for no reason with mysql_fetch_array.. but it really doesn't matter.. Bottom line, please rephrase your question Link to comment https://forums.phpfreaks.com/topic/228973-checkbox-in-a-while-loop/#findComment-1180193 Share on other sites More sharing options...
xxreenaxx1 Posted February 27, 2011 Author Share Posted February 27, 2011 Sorry, So I am using checkbox to retrieve questions and choices for these. include '../Database/take_an_exam.php'; $intNumber = 1; while($info = mysql_fetch_array( $sql )) { echo "$intNumber, {$info['Que_Question']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice1[]\" value=\"{$info['Que_Choice1']}\" /> "; echo "{$info['Que_Choice1']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice2[]\" value=\"{$info['Que_Choice2']}\" /> "; echo "{$info['Que_Choice2']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice3[]\" value=\"{$info['Que_Choice3']}\" /> "; echo "{$info['Que_Choice3']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice4[]\" value=\"{$info['Que_Choice4']}\" /> "; echo "{$info['Que_Choice4']} <br />\n"; These checkbox will be posted on to another page where I will be able to add them to a new table. But the trick is I have to get the Question Id for the question name and how would I go on to posting these values. So for example if choice 1 was ticked then its a 1 or else its a 0. For each choices I can check wherther they are ticked and if they are then its value 1 if not its 0. But not sure how to get question id. foreach ($_POST['choice1'] as $key => $val) { if(empty($val)) { $val2 = "0"; //mysql_query("INSERT INTO answer (`Ans_Answer1`)VALUES ('{$val2}'"); } else { echo "wrong"; //mysql_query("INSERT INTO answer (`Ans_Answer1`)VALUES ('{$val}'"); } } Link to comment https://forums.phpfreaks.com/topic/228973-checkbox-in-a-while-loop/#findComment-1180201 Share on other sites More sharing options...
RussellReal Posted February 27, 2011 Share Posted February 27, 2011 you could echo the names like so: choices[questionNumber][1] choices[questionNumber][2] choices[questionNumber][3] choices[questionNumber][4] for example.. while ($row = mysql_fetch_assoc($q)) { echo "$intNumber, {$info['Que_Question']} <br />\n"; for ($i =1; $i < 5; $i++) { echo "<input type=\"checkbox\" name=\"choices[{$row['id']}][{$i}]\" />{$row['Que_Choice'.$i]}"; } } and pull answers from $_POST[choices] by id $_POST[choices][idNumber] Link to comment https://forums.phpfreaks.com/topic/228973-checkbox-in-a-while-loop/#findComment-1180211 Share on other sites More sharing options...
xxreenaxx1 Posted February 28, 2011 Author Share Posted February 28, 2011 I am seriously LOST. Let me explain fully. I have a question table. In this I have Question ID, question, Choice1, Choice2, Choice 3 and choice 4. These choice are set as checkbox so if a student is taking an exam they will tick the choice which is correct and I want to post the ticked checkbox to another page and INSERT this to new table called Answer. But in this table I need the question ID. This question ID should be retrieved from the question table. My answer table needs Answer to choice 1, Answer to choice2, Anser to choice3, Answer to choice 4, Question ID and User ID. I am retrieving user id from a session and answer to choice 1 to 4 should be either 0 or 1. Depend on the checkbox. If a checkbox is ticked then its 1 if not it should be written as 0. so in order for me to set the exam I am using while loop to output the question and the choices. Now i need to post these and gather information as stated above. $intNumber = 1; while($info = mysql_fetch_array( $sql )) { "<input name=\"Que_ID[]\" value=\"{$info['Que_ID']}\" /> "; echo "$intNumber, {$info['Que_Question']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice1[]\" value=\"{$info['Que_Choice1']}\" /> "; echo "{$info['Que_Choice1']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice2[]\" value=\"{$info['Que_Choice2']}\" /> "; echo "{$info['Que_Choice2']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice3[]\" value=\"{$info['Que_Choice3']}\" /> "; echo "{$info['Que_Choice3']} <br />\n"; echo "<input type=\"checkbox\" name=\"choice4[]\" value=\"{$info['Que_Choice4']}\" /> "; echo "{$info['Que_Choice4']} <br />\n"; $intNumber++; } ?> <input type="submit" value="submit"/> Link to comment https://forums.phpfreaks.com/topic/228973-checkbox-in-a-while-loop/#findComment-1180718 Share on other sites More sharing options...
flolam Posted February 28, 2011 Share Posted February 28, 2011 If a checkbox is ticked then its 1 if not it should be written as 0. If the checkbox is ticked, the $_GET or $_POST will contain the value set in the value attribute. If its unchecked, it will not be set. So having set the checkboxes names to what RussellReal suggests, you can do this in the answer table <?php foreach ($_GET["choices"] as $question => $answers) { echo "Question id: ".$question."<br />"; echo "Answers:".implode(" - ", $answers); } ?> Link to comment https://forums.phpfreaks.com/topic/228973-checkbox-in-a-while-loop/#findComment-1180733 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.