Jump to content

Checkbox Help


baldpapa

Recommended Posts

I've created a select all question in which the user must select all of the correct answers in order to receive positive feedback.  The forms look like this...

 

  <input type="checkbox" name="question1" value="1" /> 1989<br />
  <input type="checkbox" name="question2" value="0"  /> 1991<br />
  <input type="checkbox" name="question3" value ="2" /> 1990 <br />
  <input type="checkbox" name="question4" value ="3" /> 2004</p>

 

The PHP code is setup in this manner...

 

if($question1 == 1 && $question3 == 2 && $question4 == 3)
{
echo 'Awesome';	
}
elseif ($question2 == 0 || $question1 == 1 && $question2 == 0 ||$question2 == 0 && $question3 == 2 || $question2 == 0 && $question4 == 3 )
{
echo 'Nada';	
}
elseif ($question1 == 1 && $question2 == 0 && $question3 == 2 && $question4 == 3)
{
echo 'Nope';	

 

Everything works correctly in that if the user selects all of the correct answers (values are 1-3), they receive the positive feedback.  Also, if the user selects the incorrect answer, they receive negative feedback.

 

Here's my dilemma, if the user selects all 4 checkboxes, they still receive the positive feedback.  What is the best method to ensure that if all of the answers are selected, the feedback will be negative? 

 

Thanks so much for your help!!!

Link to comment
https://forums.phpfreaks.com/topic/194913-checkbox-help/
Share on other sites

Sounds like you need to verify that the incorrect values were not also selected.

 

So, in your example:

if($question1 == 1 && $question3 == 2 && $question4 == 3 && $question2 == "")

 

If question2 was checked, it would equal 0. Not sure what the elseifs are for. Seems like it would either be correct or not?

Link to comment
https://forums.phpfreaks.com/topic/194913-checkbox-help/#findComment-1024808
Share on other sites

Here's the HTML code...

 

<body>
<p>Basic Select All Interaction</p>
<form action=quizResults.php method="post">
<p class="questionText">1. In what years did the Detroit Pistons win NBA titles?</p>
<p>
  <input type="checkbox" name="question1" value="1" /> 1989<br />
  <input type="checkbox" name="question2" value="0"  /> 1991<br />
  <input type="checkbox" name="question3" value ="2" /> 1990 <br />
  <input type="checkbox" name="question4" value ="3" /> 2004</p>
<hr />
<INPUT TYPE = "SUBMIT" value="Submit" />
<INPUT TYPE = "RESET" value="Reset"/>
</body>
</html>

 

Here's the PHP code...

 

<?php
if($question1 == 1 && $question3 == 2 && $question4 == 3)
{
echo 'Awesome';	
}
elseif ($question2 == 0 || $question1 == 1 && $question2 == 0 ||$question2 == 0 && $question3 == 2 || $question2 == 0 && $question4 == 3 )
{
echo 'Nada';	
}
elseif ($question1 == 1 && $question2 == 0 && $question3 == 2 && $question4 == 3)
{
echo 'Nope';	
}

 

Thanks again for your help!!

Link to comment
https://forums.phpfreaks.com/topic/194913-checkbox-help/#findComment-1024818
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.