【问题标题】:How to redirect to a specific URL baed on the data submitted?如何根据提交的数据重定向到特定的 URL?
【发布时间】:2020-03-12 14:00:42
【问题描述】:

我正在尝试创建一个表单,该表单在提交时会将用户重定向到一个特定的 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
}
}
?>

【问题讨论】:

    标签: php html forms post get


    【解决方案1】:

    在这里,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);">
    

    【讨论】:

    • 这正是我所需要的,并且可以确认它现在可以工作。非常感谢,
    • 您知道如何让链接在新标签页中打开而不是在同一页面中打开吗?
    • window.location.replace改成window.open
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2017-07-02
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 2011-04-10
      • 2015-02-08
      • 2012-08-03
      • 1970-01-01
      相关资源
      最近更新 更多