PDA

View Full Version : Checking Radio Buttons using PHP



Be Loud Online
27 Feb 2012, 10:52 AM
What I am trying to do is check each question is answered yes or no. If it is not, than I ask the visitor to answer the specific question they missed, and offer them a convenient back button. Then return false to stop the page from progressing...Currently, if the visitor did not answer questions 1, 4 and 8 (for example) it will catch #1, have them go back and answer, resubmit, and then catch #4, have them go back, resubmit, and then catch #6. I would ideally like to have them catch all unanswered, point them all out to the visitor at once.



Here is the code. One if statement like this for each question (with 50 questions this is the LONG way of doing it)


if (is_null($_POST["no1"]))

{

echo "You have not answer Question 1";

echo "<FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'></FORM>";

return false;

}

hyperion
27 Feb 2012, 05:28 PM
You can do a loop.


$return = array();
for($i=1; $i<=50; $i++){
if (is_null($_POST["no".$i]))
{
$return[] = $i;
}
}
if($return){
echo "You have not answer to Questions: ".implode(', ', $return);
echo "<FORM><INPUT TYPE='button' VALUE='Back' onClick='history.go(-1);return true;'></FORM>";
}else{
// everything all right, what next?
}