执行变量的操作然后保存,并使用它们到另一个位置

Perform operations of variables then save, and use them to another location

我有两个 PHP 页面,名为 index.php 和 result.php。我想在 index.php 页面中执行一些操作并为这些变量保留一些值。然后我想将它们传递到 result.php 页面。

我会在表单中使用 action="result.php" 还是使用 header('location: result.php') 传递它们?

如果你建议 header() 那么我会把 header() 代码放在哪里?

我搜索过,但没有得到任何具体结果。

两 php 页

result.php 页

<?php 
    if (isset($_POST['form1'])) {
        $right_ans = $_POST[$right_ans_count];
        $wrong_ans = $_POST[$wrong_ans_count];
        $not_answered = $_POST[$unanswered];
        echo "Right Answer: ". $right_ans;
        echo "Wrong Answer: ". $wrong_ans;
        echo "Not Answered: ". $not_answered;
    }
?>

index.php 页

    <?php
    ob_start();
?>

<?php

    if(isset($_REQUEST['form1'])) {
        $success_message = "<div class='success'>Congratulations! Your answer is right.</div>";
        $error_message = "<div class='error'>Sorry! Your answer is wrong.</div>";
        $right_ans_count = 0;
        $wrong_ans_count = 0;
        $unanswered = 0;
        $answer1 = $_POST['question1'];
        $answer2 = $_POST['question2'];
        $answer3 = $_POST['question3'];
        $answer4 = $_POST['question4'];
    }

?>

<!DOCTYPE HTML>

<html>
    <head>
        <title>Welcome to Online Model Test</title>
        <style>
            body {
                background-color: white;
            }
            .success {
                color: green;
            }
            .error {
                color: red;
            }
            table tr td h2 {
                color: cornflowerblue;
            }
            table tr td h1 {
                color: green;
            }
        </style>
    </head>
    <body>
        <form action="" method="post">
            <table>
                <tr>
                    <td><h2> What is our country name? </h2></td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question1" value="Australia">Australia
                        <input type="radio" name="question1" value="Pakistan">Pakistan
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question1" value="Bangladesh">Bangladesh
                        <input type="radio" name="question1" value="Singapore">Singapore
                    </td>
                </tr>
                <td><?php if($answer1=="Bangladesh") {
                        echo $success_message;
                        $right_ans_count++;
                    }
                    else if($answer1=="") {
                        $unanswered++;
                    }
                    else {
                        echo $error_message;
                        $wrong_ans_count++;
                    }
                    ?>
                </td>

                <tr>
                    <td><h2> What is our national flower? </h2></td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question2" value="Belly">Belly
                        <input type="radio" name="question2" value="Rose">Rose
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question2" value="Lily">Lily
                        <input type="radio" name="question2" value="Gadha">Gadha
                    </td>
                </tr>
                <td><?php if($answer2=="Lily") {
                        echo $success_message;
                        $right_ans_count++;
                    }
                    else if($answer2=="") {
                        $unanswered++;
                    }
                    else {
                        echo $error_message;
                        $wrong_ans_count++;
                    }
                    ?>
                </td>

                <tr>
                    <td><h2> What is your national fruit? </h2></td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question3" value="Mango">Mango
                        <input type="radio" name="question3" value="Lichi">Lichi
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question3" value="Orange">Orange
                        <input type="radio" name="question3" value="Jac-Fruit">Jac-Fruit
                    </td>
                </tr>
                <td><?php if($answer3=="Jac-Fruit") {
                        echo $success_message;
                        $right_ans_count++;
                    }
                    else if($answer3=="") {
                        $unanswered++;
                    }
                    else {
                        echo $error_message;
                        $wrong_ans_count++;
                    }
                    ?>
                </td>

                <tr>
                    <td><h2> Who is our national Poet? </h2></td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question4" value="Kazi Nazrul Islam">Kazi Nazrul Islam
                        <input type="radio" name="question4" value="Rejaul Islam">Rejaul Islam
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="radio" name="question4" value="Robi Thagure">Robi Thagure
                        <input type="radio" name="question4" value="Begum Rokeya">Begum Rokeya
                    </td>
                </tr>
                <td><?php if($answer4=="Kazi Nazrul Islam") {
                        echo $success_message;
                        $right_ans_count++;
                    }
                    else if($answer4=="") {
                        $unanswered++;
                    }
                    else {
                        echo $error_message;
                        $wrong_ans_count++;
                    }
                    ?>
                </td>
                <tr>
                    <td>
                        <input type="submit" name="form1" value="Submit All">
                    </td>
                </tr>
                <tr>
                    <td>
                        <h1>Result</h1> 
                        <?php if(isset($_REQUEST['form1'])) echo "Correct Answer: ". $right_ans_count. 
                            " Wrong Answer: ". $wrong_ans_count. " Unanswered: ". $unanswered; ?>
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

您 html 中的简单解决方案使用表单操作

<form action="/result.php" method="post">

所以当您提交表单时,您将在我们的 result.php 文件中获得所有 post 值 result.php

<?php 
if (isset($_POST['form1'])) {
$answer1 = $_POST['question1'];
$answer2 = $_POST['question2'];
$answer3 = $_POST['question3'];
$answer4 = $_POST['question4'];
if($answer1=="Bangladesh") {
  echo $success_message;
  $right_ans_count++;
}
  else if($answer1=="") {
  $unanswered++;
}
else {
  echo $error_message;
  $wrong_ans_count++;
}
..
.. 
}
?>

在您的 result.php 文件中,您可以检查他们的答案是否正确。