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

时间:2015-02-24 04:48:19

标签: php variables redirect

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

我会使用action =" result.php"在表单中,或者我会用标题传递它们(' location:result.php')?

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

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

两个php页面

result.php page

<?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 page

    <?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>

1 个答案:

答案 0 :(得分:0)

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

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

因此,当您提交表单时,您将获得result.php文件中的所有帖子值 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文件中,你可以检查答案是对还是错。

相关问题