繁体   English   中英

URL 根据输入重定向,但如果输入 url 不存在,则不重定向无法使 PHP 工作

[英]URL Redirect based on input but if input url does not exist do not redirect cant get PHP to work

@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。

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

谢谢你,弥迦

谢谢你,弥迦

尝试使用 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

您可以使用 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);
};

暂无
暂无

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

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