从文本输入转到页面

时间:2016-03-03 21:23:09

标签: php

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

形式:

<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/。如何解决这个问题以达到我想要的效果呢?

2 个答案:

答案 0 :(得分:1)

你可以做那样的事情。在你的情况下你只是归属目的地,你只做一个标题(...)

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

答案 1 :(得分: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

希望这有帮助!