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

时间:2014-09-14 13:25:25

标签: php redirect if-statement web conditional

我想使用php在if条件下重定向到不同的页面。(而不是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>

请帮帮我......

3 个答案:

答案 0 :(得分:4)

您在发送标题(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;
    }
?>

答案 1 :(得分:2)

如果我没错,你忘了给你的;代码提供半冒号header()

答案 2 :(得分:2)

1。检查Type.php

中的PHP代码

如果要输出标题(或使用输出缓冲来捕获它),请避免执行任何HTML输出,特别是在这种情况下,这是不必要的。

2。检查您的HTML代码:

这不是有效的HTML代码:

<input type = "radio" name = "time_type" value = "Arrival">Arrival</input>

有效版本:

<input type = "radio" name = "time_type" value ="Arrival">