Jump to content

checkbox in a while loop


xxreenaxx1

Recommended Posts

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
Share on other sites

#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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.