xn1 Posted March 20, 2011 Share Posted March 20, 2011 Hi all, Im creating a simple quiz where some of the questions are checkbox because there is more than one correct answer. What I would like to be able to do, is run an if statement to check if, say for example, options 1 and 5 out of 6 have been selected, then award the point. However the point is only awarded is both options are selected. Any suggestions? Thank you in advance Quote Link to comment Share on other sites More sharing options...
sunfighter Posted March 21, 2011 Share Posted March 21, 2011 Yes this is doable. BUT use all checkboxes because of their shape. See this example of the form used to do this in php. http://www.tizag.com/phpT/examples/formex.php Quote Link to comment Share on other sites More sharing options...
xn1 Posted March 21, 2011 Author Share Posted March 21, 2011 Because of there shape? Not sure I get what your saying? I've managed to call the array and have it display the output as 15 (assuming that checkbox 1 and 5 were selected) but how do I test that answer in an if function Quote Link to comment Share on other sites More sharing options...
sunfighter Posted March 22, 2011 Share Posted March 22, 2011 This is going to be a long reply. <html> <head> <title>My Page</title> </head> <body> <form name="myform" action="test.php" method="POST"> <div style="text-align: left;"><br> Question 1: What is you prefered drink?<br> <input type="radio" name="question1" value="1"> Water<br> <input type="radio" name="question1" value="2"> Beer<br> <input type="radio" name="question1" value="3"> Wine<br><br> Question 2: Name two ships?<br> <input type="checkbox" name="question2[]" value="1"> Nina<br> <input type="checkbox" name="question2[]" value="2"> Purple<br> <input type="checkbox" name="question2[]" value="3"> Pinta<br><br> <input type="Submit" value="Submit"> </div> </form> </body> </html> First - this is the code for the question form. It has two questions and a submit button. Question one uses radio buttons and are round. This is what I would use most of the time. Question two uses checkboxes and are square, allowing multiple boxes to be checked. (A dead give away that more than one answer is needed here). They are handled differently. Here is the php code for test.php that our form calls when we submit it. I just print (echo) the results, you will add your processing code in this place. <?php if (isset($_POST['question1'])){ $A_1 = $_POST['question1']; if($A_1 == '2'){ echo 'Correct! Remeber to ask this guy out for drinks. <br />'; }else{ echo 'Wrong, no points.<br />'; } }else{ echo 'No answer Take away Easter Vacation!!<br />'; } $test_string = ''; if(isset($_POST["question2"])){ $A_2 = $_POST["question2"]; $count = count($A_2); for($i = 0; $i < $count; $i++){ if(isset($A_2[$i])){ $test_string .= $A_2[$i]; } } if($test_string == '13'){ echo 'Correct! Boats Ahoy. <br />'; }else{ echo 'No answer Take away the boats!!<br />'; } } ?> The first thing we do is check that an answer was submitted for the question. (saves on getting errors). Then we process the answer. For question one it's simple. For question two it's a little harder. Others on this board may give you cleaner code for this, but this works. The reply is an array. '$A_2' is our array set to the $_POST submitted to us. First we find out how many elements in the array, or you can just fill that in. Next we run a for loop adding only the value of the boxes that were checked to a string, '$test_string'. Because we add to it with .= we had to declare it as blank, $test_string = ''; The $test_string holds the values of the correct answers. If that's what was check then it's correct if others were check OR more than the correct answers then we get a false results. Quote Link to comment Share on other sites More sharing options...
xn1 Posted March 22, 2011 Author Share Posted March 22, 2011 Thank you so much, that did the job perfect. I've been trying to make the questions easier to write up and have created the following (which I'm still trying to work out why it's not working), is there an easier way of storing a list of questions? <?php $catagory = "hazzard"; $qnumber = "q1"; $question = "Question 1?"; $answer = array ( "Answer 1" => "Answer 2", "Answer 3" => "Answer 4", ); echo '<table>'; echo '<tr><td width="450px"></td><td width="450px"></td></tr>'; echo '<li>.'$question'.<p>'; foreach($answer as $left=>$right){ echo '<tr><td><input type="radio" name=".'$catagory'..'$qnumber'." value=".'$left'.">.'$left'.</td><td><input type="radio" name=".'$catagory'..'$qnumber'." value=".'$right'.">.'$right'.</td></tr>'; } echo '</li></table><br><br>'; ?> Quote Link to comment Share on other sites More sharing options...
sunfighter Posted March 23, 2011 Share Posted March 23, 2011 Change echo '<li>.'$question'.<p>'; to echo '<li>'.$question.'<p>'; change echo '<tr><td><input type="radio" name=".'$catagory'..'$qnumber'." value=".'$left'.">.'$left'.</td><td><input type="radio" name=".'$catagory'..'$qnumber'." value=".'$right'.">.'$right'.</td></tr>'; TO echo '<tr><td><input type="radio" name="' . '$catagory $qnumber' . '" value="' . '$left' . '">' . $left . '</td>'; echo '<td><input type="radio" name="'.'$catagory $qnumber'.'" value="'.'$right'.'">'.$right.'</td></tr>'; Finished project is: <?php $catagory = "hazzard"; $qnumber = "q1"; $question = "Question 1?"; $answer = array ( "Answer 1" => "Answer 2", "Answer 3" => "Answer 4", ); echo '<table>'; echo '<tr><td width="450px"></td><td width="450px"></td></tr>'; echo '<li>'.$question.'<p>'; foreach($answer as $left=>$right){ echo '<tr><td><input type="radio" name="' . '$catagory $qnumber' . '" value="' . '$left' . '">' . $left . '</td>'; echo '<td><input type="radio" name="'.'$catagory $qnumber'.'" value="'.'$right'.'">'.$right.'</td></tr>'; } echo '</li></table><br><br>'; ?> Problem is with the quotes. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.