php代码运行后加载新页面


Loading new page after php code has run?

我有一个表单,这个表单位于一个名为question1.php的页面上,我希望它在按下提交按钮时加载question2.php。

<form action="question2.php" method="post">
    <input type="radio" name="answers" value="cuboid">
    <input type="radio" name="answers" value="cone">
    <input type="radio" name="answers" value="cylinder">
    <input type="radio" name="answers" value="sphere">
    <input type="submit" value="submit" name="submit">
</form>

还有php代码

<?php
    if(isset($_POST['submit'])) {
      if(isset( $_POST['ans'])) {
           $selected_answer = $_POST['ans'];
           if($selected_answer == "cuboid") {
               $_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;
           }
      }
    }
?>
编辑:我做了一个更简单的演示,试图更好地解释我自己,我有三页。

page1.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<form action="page2.php">
    <input type="submit" value="submit" name="submit">
</form>
<?php
// Set session variables
$_SESSION["favcolor"] = "green";
?>
</body>
</html>

page2.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
    <form action="page3.php" method="post">
        <input type="radio" name="answers" value="color">
        <input type="submit" value="submit" name="submit">
    </form>
<?php
// Echo session variables that were set on previous page
if(isset($_POST['submit'])) {
    if(isset($_POST['ans'])) {
        $selected_answer = $_POST['ans'];
        if($selected_answer == "color") {
            $_SESSION["favcolor"] = "red";
        }
    }
}
?>
</body>
</html>

和page3.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
     echo "Favorite color is " . $_SESSION["favcolor"] . ".";
?>
</body>
</html>

在第一页上,我声明了会话变量"favcolor",然后在第二页上,如果用户选择了单选按钮,我想把颜色更新为红色,但是它不会改变,在第三页上,它仍然打印绿色

你在page2.php上有问题,因为你在page3.php上提交表单,但你在page2.php上处理提交请求。所以我在下面为你更正了。

page1.php

<?php session_start(); ?>
<!DOCTYPE html>
<html>
  <body>
    <form action="page2.php" method="post">
        <input type="submit" value="submit" name="submit">
    </form>
    <?php
    // Set session variables
    $_SESSION["favcolor"] = "green";
    ?>
  </body>
</html>

page2.php

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
    <form action="page3.php" method="post">
        <input type="radio" name="answers" value="color">
        <input type="submit" value="submit" name="submit">
    </form>        
 </body>
</html>

page3.php

<?php session_start(); ?>
<!DOCTYPE html>
<html>
 <body>
    <?php
    // Echo session variables that were set on previous page
    if (isset($_POST['submit'])) {
        if (isset($_POST['ans'])) {
            $selected_answer = $_POST['ans'];
            if ($selected_answer == "color") {
                $_SESSION["favcolor"] = "red";
            }
        }
    }
    echo "Favorite color is " . $_SESSION["favcolor"] . ".";
    ?>
 </body>
</html>

我希望这应该工作。

$_SESSION["cuboid"] = isset($_SESSION["cuboid"]) ? $_SESSION["cuboid"] : 0;
$_SESSION["cuboid"] = ((int)$_SESSION["cuboid"]) + 1;

据我所知,您试图重定向到另一个网页。

尝试使用header()函数发送HTTP报头。

header('Location: http://example.com/page3.php');
exit();

如果HTTP头一开始不工作,使用ob_start()打开输出缓冲区

您正在检查$_POST['ans']是否在错误的页面。

当前你正在检查'ans'当用户输入page2.php,这是在用户提交包含该输入元素的表单之前。

当您在page2.php上提交表单时,该操作将请求发送到page3.php,因此您实际上希望将将颜色设置为绿色的if语句移动到page3.php

的顶部(会话开始下方)。