Using PHP to display a quiz answer

I am tying to get php to display right answer only or display the wrong answer as well as the right one.

I admit that I am not good with php I trying but having no luck so bare with me.
what I am trying or what i to do is to find if someone has answered a question right (in a PHP quiz I trying to do) or wrong.
if they have answered a question right I like to have just there right answer in my HTML page, but if they have answered it wrong I want to display not only one the answer they have selected wrong but also they right answer. this is my code

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<form method="post" action="">
<h4> Question 1<BR>Which is the port side of a vessel?</h3>
<INPUT TYPE="radio" NAME="answer1" VALUE="Left">Left.<BR><!--Correct-->
<INPUT TYPE="radio" NAME="answer1" VALUE="Right">Right.<BR />
<?php    
foreach ($_POST as $Q1){
    if ( $_POST == $Q1  ){
        break;
        echo $Q1;

    }   
            else foreach ($_POST as $Q1)  { 
                $rightAnswer1 ="left";
                if ( $_POST !== $Q1){

                    print "you have answered this wrong<br>";
                    echo " your answer".$Q1;
                    echo "<br>Correct answer" .$rightAnswer1;
                }


            };  
}


?>

<h4> Question 2<BR>The bow of a vessel is at the:</h3>
<INPUT TYPE="radio" NAME="answer2" VALUE="Front">Front.<BR><!--Correct-->
<INPUT TYPE="radio" NAME="answer2" VALUE="Back">Back.
<?php    
foreach ($_POST as $Q2){
    if ( $_POST == $Q2  ){
        break;
        echo $Q2;

    }   
            else foreach ($_POST as $Q2)  { 
                $rightAnswer1 ="left";
                if ( $_POST !== $Q2){

                    print "you have answered this wrong<br>";
                    echo " your answer".$Q2;
                    echo "<br>Correct answer" .$rightAnswer1;
                }


            };  
}


?>

<h4> Question 3<BR>The transom of a vessel is at the:</h3>
<INPUT TYPE="radio" NAME="answer3" VALUE="Front">Front.<BR>
<INPUT TYPE="radio" NAME="answer3" VALUE="Back">Back.<BR><BR><!--Correct-->
<?php    
foreach ($_POST as $Q3){
    if ( $_POST == $Q3  ){
        break;
        echo $Q3;

    }   
            else foreach ($_POST as $Q3)  { 
                $rightAnswer1 ="left";
                if ( $_POST !== $Q3){

                    print "you have answered this wrong<br>";
                    echo " your answer".$Q3;
                    echo "<br>Correct answer" .$rightAnswer1;
                }


            };  
}


?>
<INPUT TYPE="submit" VALUE="Score Quiz!" >
<BR><BR>


</form>
<body>
</body>
</html>

Your code is rather difficult to follow @deanbross.

For one thing else and foreach are separate statements. Whilst it may work as intended, writing else foreach ($_POST as $Q1) isn’t helpful in following the flow. You should have

if () {
  ...
} else {
  foreach () {
    ...
  }
}

and in your HTML, you can’t start an <h4> and end with </h3>

I realise these aren’t going to solve your problem, but it might help people find the problem.

Thank for the help Yes the <h4> just there temp unitl I get the PHP to work how I want it to

I think running through the $_POST array every time is not the best way of doing things. Firstly, I would separate the input form from checking the answers:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  ... check the answers ...
} else {
  ... show the form ...
}

In checking the answers, you would check the values of $_POST['answer1'] $_POST['answer1'] etc.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.