【问题标题】:URL Redirect based on input but if input url does not exist do not redirect cant get PHP to work基于输入的 URL 重定向,但如果输入 url 不存在,则不重定向无法使 PHP 工作
【发布时间】:2011-10-25 05:37:29
【问题描述】:

@themask 您好,感谢您的帮助,我已经扔掉了 php,现在只有这段代码

<script>
document.forms[0].onsubmit = 
    function() {
       var to = document.getElementById('myInput').value;
       var ajax = new XMLHttpRequest; 
       ajax.onreadystate = function() {
            if(this.readyState == 4 && this.status != 404) {
                window.locaiton.replace(to);
            } else {
                window.location.replace('http://www.mysite.com/incontinence/protective_underwear/presto_protective_underwear/');
            }
    }; 
    ajax.open('GET',to);
    ajax.send(null);
};
      </script>
      <form onsubmit="location.href='http://www.mysite.com/coupons/' + document.getElementById('myInput').value; return false;" >
  <input type="text" id="myInput" />
  <input name="Submit" type="submit" id="Submit" />
</form>

但如果使用了错误的代码,它仍然会将我发送到不正确的网址。它就像是一起跳过了java。

任何额外的帮助都会很棒。

谢谢你, 弥迦

谢谢你, 弥迦

【问题讨论】:

    标签: javascript forms url redirect error-handling


    【解决方案1】:

    尝试使用 get_headers()

    $headers = get_headers('http://www.activelifemed.com/incontinence/protective_underwear/presto_protective_underwear/')
    if($headers[0] == 'HTTP/1.1 200 OK'){
      //exists
    }
    else{
      //doesn't exist
    }
    

    http://www.php.net/manual/en/function.get-headers.php

    【讨论】:

    • 我认为最好检查 HTTP CODE 是否不同于 404
    • @The Mask 你好,我还是迷路了。我已经扔掉了 php,现在有了
    【解决方案2】:

    您可以使用 JavaScript 来实现:

    document.forms[0].onsubmit = 
        function() {
           var to = document.getElementById('myInput').value;
           var ajax = new XMLHttpRequest; 
           ajax.onreadystate = function() {
                if(this.readyState == 4 && this.status != 404) {
                    window.locaiton.replace(to);
                } else {
                    window.location.replace('http://www.mysite.com/incontinence/protective_underwear/presto_protective_underwear/');
                }
        }; 
        ajax.open('GET',to);
        ajax.send(null);
    };
    

    【讨论】:

    • @micah,您不应该编辑某人的答案来评论某事。您可以添加评论或编辑您的问题。
    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 2011-12-25
    • 2021-01-07
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    相关资源
    最近更新 更多