繁体   English   中英

如何根据提交的数据重定向到特定的 URL?

[英]How to redirect to a specific URL baed on the data submitted?

我正在尝试创建一个表单,该表单在提交时将用户重定向到一个特定的 URL,该 URL 在 URL 的末尾包含提交的内容。

因此,例如,像这样的简单表单:

<form method="post">
    <input type="text" name="tracking">
    <input type="submit" name="submit" value="submit">
</form>

当用户输入“abc”作为跟踪号并点击“提交”时,他们将被重定向到:

https://www.specificurl.com/abc

我的问题是,这可能吗,如果可以,怎么做?

这是我到目前为止...

在表单页面上:

<form action="redirect_form.php" id="#form" method="post" name="#form">
<label>Enter your tracking code:</label>
<input id="tracking" name="tracking" placeholder='Enter your tracking code' type='text'>
<input id='btn' name="submit" type='submit' value='Submit'>
<?php include "include/redirect.php"; ?>
</form>

包含在 redirect.php 文件中:

<?php
if(isset($_POST['submit'])){
// Fetching variables of the form which travels in URL
$name = $_POST['tracking'];
if($tracking)
{
//  To redirect form on a particular page
header("Location:https://specificurl.com/$tracking");
}
else{
?><span><?php echo "Please enter tracking number.";?></span> <?php
}
}
?>

在这里 JavaScript 可能就足够了:

<input type="text" id="tracking" name="tracking">
<input type="button" name="submit" value="submit" onclick="window.location.replace('https://www.specificurl.com/'+tracking.value);">

您应该为此使用 GET 方法。 使用 javascript,您可以提取文本输入并操作用户重定向到的 url。 希望对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM