要重定向的页面取决于多个“选择”选项

Page to redirect dependent on multiple 'select' choices

本文关键字:选择 选项 取决于 重定向      更新时间:2023-09-26

我正在尝试根据两个select选择将页面重定向到相关页面。我不确定这是否最好用 Javascript 或 PHP 完成,因为我希望能够轻松更改页面 - 这让我认为某种 PHP 表单处理程序是最好的。

JSFiddle

任何关于如何工作的例子将不胜感激。我做了一个JSFiddle来帮忙。如果我不够具体,如果有人选择"工程",然后选择"新南威尔士",我会尝试将页面重定向到某个页面,如果他们选择"健康"然后选择"新南威尔士"等,我会尝试将页面重定向到某个页面。

提前谢谢。

更干净的解决方案是

<?php
if ($_POST['specialist'] == "engineering" && isset($_POST['state'])){
    switch($_POST['state']){
        case "act":
            header("Location: act.php");
        break;
        case "nsw":
            header("Location: nsw.php");
        break;
        case "nt":
            header("Location: nt.php");
        break;
        default:
            // Redirect to a default page if not matched or some other message
        break;
    }
}
?>

这样,您就不必每次都检查工程师。您可以为不同的职业制作多个这些。此外,最好在重定向后使用 exit();break;,在这种情况下实现。

您可以通过获取$_POST变量并直接使用它来使其更快,尽管这很容易被其他人滥用,因此请确保检查可用值。

<?php
$engineering_states = array('act', 'nsw', 'nt', 'qld', 'sa', 'tas', 'vic', 'wa');
if($_POST['specialist'] == "engineering"){
    if(in_array($_POST['state'], $engineering_states)){
        header("Location: $_POST['state'].php");
    }
}
?>

找到了一个解决方案,@Sean的评论提供了大部分,我只是稍微调整了代码。工作 PHP:

<?php if ($_POST['specialist'] == "engineering" && $_POST['state'] == "act"){ header("Location: act.php");} else if ($_POST['specialist'] == "engineering" && $_POST['state'] == "nsw"){ header("Location: nsw.php");} else if ($_POST['specialist'] == "engineering" && $_POST['state'] == "nt"){ header("Location: nt.php");} ...

我的方法是创建一个数组(将来也可以轻松地用数据库替换)。

<?php
$a = Array(
    "engineering" => Array( "act"=>"enggact.php",
                            "nsw"=>"enggnsw.php",
                            "nt"=>"enggnt.php"
                          ),
    "health" => Array(  "act"=>"heltact.php",
                            "nsw"=>"heltnsw.php",
                            "nt"=>"heltnt.php"
                          ),
    "information-technology" => Array(  "act"=>"itact.php",
                            "nsw"=>"itnsw.php",
                            "nt"=>"itnt.php"
                          )
);
if(isset($a[$_POST['specialist']][$_POST['state']])) {
    header("Location:".$a[$_POST['specialist']][$_POST['state']]);
}
?>

希望这有帮助