Javascript AJAX和PHP-需要在满足某些条件后重定向PHP页面

Javascript AJAX和PHP-需要在满足某些条件后重定向PHP页面,javascript,php,ajax,redirect,Javascript,Php,Ajax,Redirect,好的,我有这个问题,我还不太擅长Ajax,所以我真的不知道如何继续 我有一个页面,每7秒使用Ajax和Javascript运行一次刷新。 每次Ajax页面刷新时,它都会从数据库中的SQL值中删除一个。 当该值变为0时,我需要主php页面转到其他URL 下面的代码位于streets.php上。 它调用streetsdo.php来显示信息 <script> function findUser(str) { var dapage='streetsdo.php'; v

好的,我有这个问题,我还不太擅长Ajax,所以我真的不知道如何继续

我有一个页面,每7秒使用Ajax和Javascript运行一次刷新。 每次Ajax页面刷新时,它都会从数据库中的SQL值中删除一个。 当该值变为0时,我需要主php页面转到其他URL

下面的代码位于streets.php上。 它调用streetsdo.php来显示信息

<script>    
function findUser(str) {
    var dapage='streetsdo.php';
    var http;
    var myself = arguments.callee;
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            http = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            http = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
    if (http) {
        http.onreadystatechange = function() {
            if (/4|^complete$/.test(http.readyState)) {
                document.getElementById('result').innerHTML = http.responseText;
                setTimeout(function(){myself();}, 7000);
            }
        };
        http.open('POST',dapage,true);
        http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        http.send('req=".$uid."&req2=numb');

    }

}
</script>
这就是执行刷新的代码。 在streetsdo.php上,MySQL查询基本上是倒计时。 一旦它达到0,我需要streets.php转到另一个URL

这可能吗

我尝试使用headerLocation:blabla,但这不起作用,因为它只重定向streetsdo.php页面,而不重定向脚本运行的streets.php,我陷入了框架中

编辑此脚本由streets.php上的按钮激活

<span id='result'><span class='Streets'>
<form method='post' action='streets.php'>

<input type='button' class='theButton' value='Start' onclick='findUser(this.value)'></form>
</span>
请尝试以下脚本:

`<script type="text/javascript">
var myTimer;
function ajaxData(){
    var result = document.getElementById("result");

    var hr = new XMLHttpRequest();
    hr.open("POST", "your php file.php", true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var d = hr.responseText;
            for(var o in d){
                if(d[o].id){
                    result.innerHTML += '<p>'+d[o].id+'<br></p>';

                }
            }
        }
    }
    hr.send("your variable which the php will take as $_POST[]");
    result.innerHTML = "requesting...";
    myTimer = setTimeout('ajax_json_data()',7000);

}
</script>`

当倒数为零时,ajax调用必须返回一个特殊值。你检查一下它

document.getElementById('result').innerHTML = http.responseText;


我不认为这会像作者要求的那样重定向页面,只是清理代码。我不认为这是我要寻找的。因此我应该将其添加到streets.php上的原始脚本中,以便如果它识别完成的响应,它将重定向,对吗?非常感谢您的帮助。我只是不知道如何将响应返回到streets.php。我是不是应该把它回复过来?就是这样。你太棒了,非常感谢!这对我来说现在更有意义了php在倒计时时返回您的内容。当它达到0时,is返回触发重定向的特殊值
if(http.responseText == 'finished'){
    window.location.href = '/myotherpage.php'
} else{
    document.getElementById('result').innerHTML = http.responseText;
}