如何将页面重定向到“示例.php”

How do I redirect a page to "example.php"?

本文关键字:示例 php 重定向      更新时间:2023-09-26

我有一个简单的项目要编码。在主页上,我有一些图像。在后台,我使用 Ajax 每 5 秒查询一次名为"check.php"的文件。在检查中.php从数据库中获取一些数据,如果数据等于 0,我想将整个站点重定向到"example.php",但如果数据等于 1,我只想用一个名为"无变化"的文本更新 ajax 响应div。

问题:每当我尝试这样做时,它都不会重定向到"示例.php",而是在 ajax 响应div 中显示示例.php。我正在使用标题("位置:示例.php"); 为此。

我的代码:

//function auto for ct fetch starts
function reload_ct() {

var xmlhttp;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("ct").innerHTML=xmlhttp.responseText;
    }
  }
     var c_user = document.getElementById('c_name').value;
   xmlhttp.open("GET","check.php?cu=" + c_user,true);
xmlhttp.send();
}
window.setInterval(reload_ct, 100);
//function auto for ct fetch ends

我会将重定向组合从 AJAX php 文件传递回原始文件

返回("转到示例.php");

然后在原始文件中,您获取此数据并进行 JS 重定向,如果它 = "转到示例.php"。

这应该有效(在jQuery中):

$.post('check.php',{'c_user':c_user})
$.done(function(data) {
if(data == '0'){
window.location.href = "example.php";
}
});

一般格式为

$.post('URL_SENDING_TO',function (data returned from request) {
dosomething();
});

如果要发送数据,可以使用 .done ,如下所示:

$.post('URL_SENDING_TO',{'datatosend':'value1','datatosend2':'value2'})
.done(function (data returned from request) {
dosomething();
});

当然,如果你想在没有jQuery的情况下做到这一点,这应该可以工作(参考 http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp 和 http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_database):

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        if (xmlhttp.responseText == '0') {
            window.location.href = "example.php";
        }
    }
}
var time_five_sec = setInterval(
    function () { 
        xmlhttp.open("POST", "check.php", true);
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.send("c_user=" + c_user);
    }, 5000);

如果你想将最后一个函数更改为 setTimeout(如 Jasper 建议的那样),你可以这样写:

function timed_ajax_request() {
    setTimeout(function () { ajaxrequest() }, 5000);
}
function ajaxrequest() {
    xmlhttp.open("POST", "check.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("c_user=" + c_user);
    timed_ajax_request();
}
timed_ajax_request();

这将在循环中发送它,它首先调用函数timed_ajax_request,然后在发送 ajax 请求时,它再次调用timed_ajax_request,因为它是一个Timeout,它将等待 5000 毫秒(或 5 秒)在调用请求之前。

如果要使用用户GET而不是POST,更改行:

    xmlhttp.open("POST", "check.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("c_user=" + c_user);

自:

    xmlhttp.open("GET", "check.php?c_user=" + c_user, true);
    xmlhttp.send("c_user=" + c_user);

这应该同样有效。这同样适用于jQuery,将其从:

$.post('check.php',{'c_user':c_user})
$.done(function(data) {
if(data == '0'){
window.location.href = "example.php";
}
});

自:

$.get('check.php',{'c_user':c_user})
$.done(function(data) {
if(data == '0'){
window.location.href = "example.php";
}
});

所以,总而言之,要做你想在你的问题中做的事情,包括 Jasper 的建议,你会写:

var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        if (xmlhttp.responseText == '0') {
            window.location.href = "example.php";
        }
    }
}
function timed_ajax_request() {
    setTimeout(function () { ajaxrequest() }, 5000);
}
function ajaxrequest() {
    xmlhttp.open("GET", "check.php?cu=" + c_user, true);
    xmlhttp.send();
    timed_ajax_request();
}
timed_ajax_request();