【问题标题】:URL Redirect based on input but if input url does not exist send back to same page基于输入的 URL 重定向,但如果输入 url 不存在,则发送回同一页面
【发布时间】:2011-10-25 03:09:30
【问题描述】:

这段代码几乎可以满足我的需要:

<form onsubmit="location.href='http://www.mysite.com/' + document.getElementById('myInput').value; return false;">
  <input type="text" id="myInput" />
  <input type="submit" />
</form>

唯一缺少的是,如果他们的用户输入了一个不存在的输入,我希望它让他们无处可去或重定向回同一页面。现在它会将他们带到错误页面,他们必须使用后退按钮返回并重试。
任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: forms url redirect input error-handling


    【解决方案1】:

    您能否编写一个小的 PHP 脚本用作表单操作?在此脚本中,您可以检查提供的用户输入并验证 URL 是否存在。如果是这样,您可以将header() 重定向到给定页面。如果它不存在,您可以将它们重定向到所需的页面。

    <?php
    $input = $_POST['myInput']; // You'll also need to add in a name='' tag in the HTML!
    $desired_page = ''; // page you want to go to if inputted file cannot be found
    
    if (file_exists($input)) {
      header("Location: $input");
    }
    else {
      header("Location: $desired_page");
    }
    

    它需要大量改进,但就是这样。那会做你想做的吗?

    【讨论】:

    • 你好皮特,我知道你的方向是正确的。我已经添加了您在 php 文件中提供的代码和 html 中的操作。但它不起作用。我一定还是错过了什么。任何额外的帮助将不胜感激。谢谢
    • 请发布您的代码 - PHP 和 HTML - 我会看看 :)
    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 2011-12-25
    • 2017-08-11
    • 1970-01-01
    • 2011-07-11
    • 2017-10-28
    • 1970-01-01
    • 2020-11-12
    相关资源
    最近更新 更多