简体   繁体   中英

my other if else statement is executing php

I have 3 forms with 3 radios each but executed one at a time because of random generator. If I click a radio, it will go to next page and will be checked by the if else statement. Now my problem is, when it gets answered and it's correct, it will output yes. However, my other if else statement of my other radio executes. Since it's not answered, it assumed that it was wrong, so it outputted no.

here's my code:

<!doctype html>
<html>
<head>
    <title>Question and Answer</title>
</head>

<body>

    <?php 
        //Creating random numbers
        $rid = rand(1,3);
    ?> 




    <?php 


    if ($rid == 1){

echo "

<form action='answer.php?id=1' method='post' id='quizForm' id='1'>


<ol>


    <li>
    <h3>What does HTML Stands For ?</h3>

    <div>
    <input type='radio' name='answerOne' id='answerOne' value='A' />
    <label for='answerOneA'>A) Hyper text markup language</label>
    </div>

    <div>
    <input type='radio' name='answerOne' id='answerOne' value='B' />
    <label for='answerOneB'>B) Hyper turn mark lingo</label>
    </div>

    <div>
    <input type='radio' name='answerOne' id='answerOne' value='C' />
    <label for='answerOneC'>C) Happy tissue mahatma life</label>
    </div>
    </li>




<input type = 'submit' name = 'submit1' value = 'Choose..'> 


</form>";

}


    if ($rid == 2){

echo "

<form action='answer.php?id=1' method='post' id='quizForm' id='1'>


<ol>


    <li>
    <h3>What does CSS Stands For ?</h3>

    <div>
    <input type='radio' name='answer2' id='answer2' value='A' />
    <label for='answer2A'>A) College Computer Studies</label>
    </div>

    <div>
    <input type='radio' name='answer2' id='answer2' value='B' />
    <label for='answer2B'>B) Cascading Style Sheet</label>
    </div>

    <div>
    <input type='radio' name='answer2' id='answer2' value='C' />
    <label for='answer2C'>C) Cascaded Style Sheet</label>
    </div>
    </li>




<input type = 'submit' name = 'submit1' value = 'Choose..'> 


</form>";

}

if ($rid == 3){

echo "

<form action='answer.php?id=1' method='post' id='quizForm' id='1'>


<ol>


    <li>
    <h3>What does PHP Stands For ?</h3>

    <div>
    <input type='radio' name='answer3' id='answer3' value='A' />
    <label for='answerOneA'>A) Hyper text markup language</label>
    </div>

    <div>
    <input type='radio' name='answer3' id='answer3' value='B' />
    <label for='answerOneB'>B) Hyper turn mark lingo</label>
    </div>

    <div>
    <input type='radio' name='answer3' id='answer3' value='C' />
    <label for='answerOneC'>C) Happy tissue mahatma life</label>
    </div>
    </li>




<input type = 'submit' name = 'submit1' value = 'Choose'> 


</form>";

}

    ?> 



</body>
</html>

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<!doctype html>
<html>
<head>
    <title>Question and Answer</title>
</head>

<body>

    <?php

        error_reporting(E_ALL ^ E_NOTICE);

        $answer = array('A','B','C');


        $answer1= $_POST['answerOne'];
        $answer2= $_POST['answer2'];

        if($answer1 == $answer[0]){
            echo 'yes';
        }
        else if ($answer1 != $answer[0]){
            echo 'no';
        }
        else{

        }

        if($answer2 == $answer[1]){
            echo 'yes';
        }
        else if ($answer2 != $answer[1]){
            echo 'no';
        }

    ?>

</body>
 </html>

Here is a cleaner way to do it. Inside is_correct function, it check if particular answer exist in $_POST , if so it returns if the answer is correct:

function is_correct()
{
    $answer = array(
        'answerOne' => 'A',
        'answer2' => 'B',
        'answer3' => 'C',
    );

    foreach ($answer as $k => $v) {
        if (array_key_exists($k, $_POST)) {
            return $v == $_POST[$k];
        }
    }

    return false;
}

if (is_correct()) {
    echo "yes";
} else {
    echo "no";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM