对变量执行操作,然后保存,并将它们用于其他位置


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

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

我会在表单中使用action="result.php",还是将它们与头("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文件中,您可以检查答案是否正确。