我正在尝试制作一个简单的表单,当输入某些文本时,它会重定向到另一个页面。
形式:
<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/
。如何解决这个问题以达到我想要的效果呢?
答案 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
希望这有帮助!