PHP - 使用 if 条件重定向到不同的页面

Thi*_*ruG 4 php redirect conditional if-statement web

我想使用 php.(不是 javascript)在 if 条件下重定向到不同的页面,我在这里尝试作为示例。但不幸的是它对我不起作用。:/这是我的代码:

<html>
    <body>
        <?php
            $time_type = $_POST['time_type'];
            $driver_type = $_POST['driver_type'];

            if (strcmp($time_type, "Arrival") && strcmp($driver_type, "Customer") )
            {
                header('location: http://localhost:8080/Project/Enter_Details.html');
                exit();
            }
        ?>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这个 php 链接到下面的 html 页面。

<html>
    <title> Shopping Complex</title>

    <body>
        <form name = "Type" action = "Type.php" method = "POST">
            <fieldset>
                <table>
                    <p>
                        <tr>
                            <td><input type = "radio"
                                   name = "time_type" value = "Arrival">Arrival</input>
                                <input type = "radio"
                                   name = "time_type" value = "Departure">Departure</input>
                            </td>
                        </tr>
                        <tr>
                            <td><input type = "radio"
                                   name = "driver_type" value = "Staff">Staff</input>
                                <input type = "radio"
                                   name = "driver_type" value = "Customer">Customer</input>
                            </td>
                        </tr>
                    </p>
                    <p>
                        <tr>
                            <td><input type = "submit"
                                       value = "Go" id = "buttonId"></input>
                            </td>
                        </tr>
                    </p>
                </table>
            </fieldset>
        </from>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

请帮我摆脱这个......

Ste*_*inn 5

您在发送标头(PHP 标签)之前发送数据(HTML 标签)

为了让它工作,使 PHP 代码成为文档中的第一件事,而不是任何 HTML。所以改变这个(也修正了上面提到的 $driver_type 和下面缺少的分号错误):

<html>
    <body>
        <?php
            $time_type = $_POST['time_type'];
            $driver_type = $_POST['driver_type'];

            if (strcmp($time_type, "Arrival") && strcmp($driver_type, "Customer") )
            {
                header('location: http://localhost:8080/Project/Enter_Details.html');
                die;
            }
        ?>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

仅此而已:

<?php
    $time_type = $_POST['time_type'];
    $driver_type = $_POST['driver_type'];

    if (strcmp($time_type, "Arrival") && strcmp($driver_type, "Customer") )
    {
        header('location: http://localhost:8080/Project/Enter_Details.html');
        die;
    }
?>
Run Code Online (Sandbox Code Playgroud)