【问题标题】:PHP - using if conditions to redirect to different pagesPHP - 使用 if 条件重定向到不同的页面
【发布时间】:2014-11-08 03:13:15
【问题描述】:

我想使用 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>

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

【问题讨论】:

  • 您的程序中有错字strcmp($diver_type, "Customer") 更改为$driver_type。同时回显 $_POST 变量。
  • 它是 strcmp($driver_type, "Customer") 而不是 strcmp($diver_type, "Customer")。在此旁边,您打开htmlbody 标签,这是输出...不要打开那些。
  • @Dorvalla 是的。谢谢.....我已经更正了。
  • 提示一下,strcmp 是二进制比较,所以如果计算为相等,它将返回 0 并被强制转换为 false。否则,如果字符串不同,它将返回 1(大于)或 -1(小于)。这两种情况都将被强制转换为 true。

标签: php redirect if-statement web conditional


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    如果我没记错的话,你忘了给你的header() 代码加上分号;

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-03-11
        • 1970-01-01
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-25
        • 1970-01-01
        • 2019-02-04
        相关资源
        最近更新 更多