【问题标题】:Go to page from text input [duplicate]从文本输入转到页面[重复]
【发布时间】:2016-03-03 21:23:08
【问题描述】:

我正在尝试制作一个简单的表单,当输入某些文本时,它会重定向到另一个页面。

表格:

<form action="goto.php" method="post">
Destination: <input type="text" name="destination">
<input type="submit">
</form>

我不确定如何设置goto.php 以达到预期的效果。我基本上想要以下内容:

<?php 
if ($_POST["destination"]="mail" ) {
    header( 'Location: /mail/' );
} elseif ($_POST["destination"]="forms") {
    header( 'Location: /forms/' );
} else {
    echo "Invalid";
}
?>

但是,这不起作用,因为无论输入什么文本,header(); 的工作方式都会使表单转到 /mail/。我该如何解决这个问题以达到我想要的结果?

【问题讨论】:

标签: php


【解决方案1】:

您正在分配 'mail'$_POST["destination"],它返回 true,因此 if 有效

改为这样做:

<?php 
if ($_POST["destination"] =="mail" ) {//Note the ==
    header( 'Location: /mail/' );
} elseif ($_POST["destination"]=="forms") { //Note the ==
    header( 'Location: /forms/' );
} else {
    echo "Invalid";
}
?>

有关比较运算符的更多信息,请参阅this

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你可以做这样的事情。在您的情况下,您只需将目的地归因于您只制作一个标题(...)

    <?php 
    if ($_POST["destination"] === "mail" ) {
        $destination = '/mail/';
    } elseif ($_POST["destination"] === "forms") {
        $destination = '/forms/';
    } else {
        echo "Invalid";
        return;
    }
    header( 'Location: ' . $destination );
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 2020-01-13
      • 2013-08-01
      • 2022-06-27
      • 2023-02-26
      • 2017-02-06
      • 2019-03-23
      相关资源
      最近更新 更多