Home > Back-end >  PHP Posting variables to another page after calculation
PHP Posting variables to another page after calculation

Time:02-14

i'm trying to post my variables "$correct" and "$incorrect" over to another page call "testresult.php". But i can't seem to receive the variables on the other page. Is there something wrong with my codes?

<form action="testresult.php" method="POST">
    <label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
    <input type="text" id="question1_answer" name="question1_answer" required><br><br>
    
    <label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
    <input type="text" id="question2_answer" name="question2_answer" required><br><br>

    <label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
    <input type="text" id="question3_answer" name="question3_answer" required><br><br>

    <input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
    <input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" />
    
    <input type="submit" name="submitAnswers" value="Submit">
</form>

<?php
        if(isset($_POST["submitAnswers"])){
            $correct = 0;
            $incorrect = 0;
            $qn1_ans = $_POST['question1_answer'];
            $qn2_ans = $_POST['question2_answer'];
            $qn3_ans = $_POST['question3_answer'];

            if ($qn1_ans == $array_answer[$question_num_array[0]]){ $correct   ; }
            else { $incorrect   ; }

            if ($qn2_ans == $array_answer[$question_num_array[1]]){ $correct   ; }
            else { $incorrect   ; }

            if ($qn3_ans == $array_answer[$question_num_array[2]]){ $correct   ; }
            else { $incorrect   ; }

        }
    ?>

On the other page, this is my code.

<html>
    <head>
    <title>Results</title>
    </head>
    <body>
        <?php
        $correct = $_POST['correct'];
        $incorrect = $_POST['incorrect'];

        echo $correct."test ".$incorrect;
        ?>
    </body>
</html>

CodePudding user response:

Your first page should JUST show the questions, it will be like this.

<form action="testresult.php" method="POST">
    <label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
    <input type="text" id="question1_answer" name="question1_answer" required><br><br>
    
    <label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
    <input type="text" id="question2_answer" name="question2_answer" required><br><br>

    <label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
    <input type="text" id="question3_answer" name="question3_answer" required><br><br>

    <!--input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
    <input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" /-->
    
    <input type="submit" name="submitAnswers" value="Submit">
</form>

When you hit the submit button, the answers are posted to testresult.php.

In this file, first checking the answers, then showing the result.

The page doesn't contain questions, so you need to include the php file too.

<html>
    <head>
    <title>Results</title>
    </head>
    <body>
        <?php
        if(isset($_POST["submitAnswers"])){
            include 'question.php'; #include the question array.

            $correct = 0;
            $incorrect = 0;
            $qn1_ans = $_POST['question1_answer'];
            $qn2_ans = $_POST['question2_answer'];
            $qn3_ans = $_POST['question3_answer'];

            if ($qn1_ans == $array_answer[$question_num_array[0]]){ $correct   ; }
            else { $incorrect   ; }

            if ($qn2_ans == $array_answer[$question_num_array[1]]){ $correct   ; }
            else { $incorrect   ; }

            if ($qn3_ans == $array_answer[$question_num_array[2]]){ $correct   ; }
            else { $incorrect   ; }

            echo $correct."test ".$incorrect;
        }
        ?>
    </body>
</html>

CodePudding user response:

Where did those variables from from?

In your code you're calling two vars $array_answer and $question_num_array.

None of these two variables are set! Please fix this issue.

CodePudding user response:

The Form action you've set to your testresult.php. So surely the form will carry all of its data to that page. But you are trying to get the data on the same page as if(isset($_POST["submitAnswers"])) where is the form; what actually should exist in that testresult.php page. So should be like this:

index.php or something.php

<form action="testresult.php" method="POST">
    <label for="qn1">Question 1: <?php echo $array_question[$question_num_array[0]] ?></label><br>
    <input type="text" id="question1_answer" name="question1_answer" required><br><br>
    
    <label for="qn2">Question 2: <?php echo $array_question[$question_num_array[1]] ?></label><br>
    <input type="text" id="question2_answer" name="question2_answer" required><br><br>

    <label for="qn3">Question 3: <?php echo $array_question[$question_num_array[2]] ?></label><br>
    <input type="text" id="question3_answer" name="question3_answer" required><br><br>

    <input type="hidden" name="correct" id="correct" value="<?php $correct ?>" />
    <input type="hidden" name="incorrect" id="incorrect" value="<?php $incorrect ?>" />
    
    <input type="submit" name="submitAnswers" value="Submit">
</form>

testresult.php

<?php



// questions array that is called in the form's page should be 
   called here too to get the questions and comparisons.

    if (isset($_POST['submitAnswers'])) {
    $correct = 0;
    $incorrect = 0;
    $qn1_ans = $_POST['question1_answer'];
    $qn2_ans = $_POST['question2_answer'];
    $qn3_ans = $_POST['question3_answer'];

    if ($qn1_ans == $array_answer[$question_num_array[0]]) {
        $correct  ;
    } else {
        $incorrect  ;
    }

    if ($qn2_ans == $array_answer[$question_num_array[1]]) {
        $correct  ;
    } else {
        $incorrect  ;
    }

    if ($qn3_ans == $array_answer[$question_num_array[2]]) {
        $correct  ;
    } else {
        $incorrect  ;
    }
}
?>
<html>

<head>
    <title>Results</title>
</head>

<body>
    <?php
    $correct = $_POST['correct'];
    $incorrect = $_POST['incorrect'];
    
    echo $correct . 'test ' . $incorrect;
    ?>
</body>

</html>
  • Related