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

标签 php redirect if-statement web conditional

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

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

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

请帮我解决这个问题......

最佳答案

您在发送 header (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>

仅此而已:

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

关于PHP - 使用 if 条件重定向到不同的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25833723/

相关文章:

php - SQLSTATE[01002] Adaptive Server 连接失败(严重性 9)

php - curl 错误 58 : SSL: Can't load the certificate "..." and its private key: OSStatus -25299 on Mac

javascript - 使用 React 自动重定向?

调用直接 url 路径时,Angular 4 路由不起作用

检查数组是否包含 C 中另一个数组的所有数组值

javascript - 在 php if 语句中使用 javascript 变量

php - 变量在 php 中的 mysql 查询内无法正常工作

PHP:需要一个 eval() 的替代方法来动态构建多维数组

Javascript:回文函数?

Azure 应用程序网关 - 将流量重定向到外部站点而不更改 URL