Javascript 使用PHP检测要重定向到特定页面的输入内容

Javascript 使用PHP检测要重定向到特定页面的输入内容,javascript,php,html,redirect,Javascript,Php,Html,Redirect,我的HTML中有以下内容: <input type="text" name="code" /> <input type="submit" value="Submit" /> 此外,这是否正确(对于PHP)?(还没试过) 请容忍我,因为我在这个领域还是个新手 谢谢。试试这个 $(document).ready(function(){ var pages = { "123456" : "page1.html", "56789" : "page2.html",

我的HTML中有以下内容:

<input type="text" name="code" />
<input type="submit" value="Submit" />
此外,这是否正确(对于PHP)?(还没试过)

请容忍我,因为我在这个领域还是个新手

谢谢。

试试这个

$(document).ready(function(){
var pages = {
    "123456" : "page1.html",
    "56789" : "page2.html",
    "5252" : "page3.html"
}
$('input[type=submit]').click(function (e) {
e.preventDefault();
var val = $('input[name=code]').val();
var page = pages[val];

 window.location = page;

 });
 });

检查表单提交(_POST或_GET),然后根据“text”的值重定向。请记住,只有在没有输出发送到浏览器时,才能执行位置重定向。。之后总是“退出”

if(isset($_POST)){

    if($_POST['text'] == '12345'){

        header ("Location: page1.html");
        exit;

   } elseif($_POST['text'] == '6789'){

        header ("Location: page2.html");
        exit;

   } 

}
用PHP实现这一点

<form action="file.php" method="POST">
    <input type="text" name="code" />
    <input type="submit" value="Submit" />
</form>
或者更优雅的解决方案

$page = 'codenotfound.html';

$pages = array(
    '123456' => 'page1.html',
    '56789' =>'page2.html',
    '5252' => 'page3.html'
);

if(isset($pages[$_POST['code']])):
    $page = $pages[$_POST['code']];
endif;

header('Location: ' . $page);

非常感谢。这似乎是完美的工作。由于我对JS/jQuery不太了解,我想问您,当语句仅使用
if
else
时,如何在字段中添加更多值?是否要在表单上添加更多字段?不,仅添加值。123456用于page1.html,56789用于page2.html,5252用于page3.html,等等:)根据您的要求更新了答案如果您觉得这有用,请接受答案只是一个旁注:在javascript中这样做是“危险的”,如果您想有一个清晰的想法,则不安全,因为用户将始终能够检查您的javascript并准确地获取目标url。也许除非我使用
并禁止热链接。:)
<form action="file.php" method="POST">
    <input type="text" name="code" />
    <input type="submit" value="Submit" />
</form>
switch($_POST['code']):

    case '123456':
        header('Location: page1.html');
    break;

    case '56789':
        header('Location: page1.html');
    break;

    case '5252':
        header('Location: page3.html');
    break;

    default:
        header('Location: codenotfound.html');

endswitch;
$page = 'codenotfound.html';

$pages = array(
    '123456' => 'page1.html',
    '56789' =>'page2.html',
    '5252' => 'page3.html'
);

if(isset($pages[$_POST['code']])):
    $page = $pages[$_POST['code']];
endif;

header('Location: ' . $page);