Javascript等待PHP函数返回

Javascript Wait for PHP function to return

本文关键字:返回 函数 PHP 等待 Javascript      更新时间:2023-09-26

我有一个javascript函数,要求它在继续之前验证数据库中的一些信息。这是一个简单的布尔条件。如果为true,则继续,否则终止。问题似乎是javascript函数立即将函数调用求值为"false",从而终止执行。它在函数实际返回之前执行,因此即使内部函数返回true,主函数也会错误地终止。

我对js、php和ajax还很陌生。我猜这是异步php调用的结果。我的结论是对的吗?如果是,有没有办法让javascript函数暂停,直到它收到函数调用的结果?

这是我的密码。谢谢你的建议。

首先,使用按钮onclick(在文件"users.php"中)调用的主要javascript函数:

function deleteUser() {
            //check for excessive inactivity
            if (verifySession()) {
                console.log("verified");
            } else {
                console.log("verification failed");  //<--line 33
                return false;
            }
            //continue on...

这是if语句(在文件"session_handler.js"中)调用的函数:

function verifySession() {
    var xmlhttp = new XMLHttpRequest();
                
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            var response = xmlhttp.responseText;
            console.log(response);                     //<--line 11
            if (response != "verified") {
                window.location.href = "signin.html";
                return false;
            } else {
                return true;
            }
        }
    }
            
    xmlhttp.open("GET", "scripts/verifySession.php", true);
    xmlhttp.send();
    return false;
}

这就是输出:

已验证session_handler.js:11

验证失败的用户.php:33

因此,您可以看到if条件输出字符串"verified",因此返回"true"结果。但主要函数将其评估为"false",字符串输出"验证失败"就是明证。我能做些什么来纠正这个问题?

谢谢!

这是因为xmlhttp.onreadystatechange=function()返回true,而verifySession()在此处返回false

xmlhttp.open("GET", "scripts/verifySession.php", true);
xmlhttp.send();
return false;

我的想法是这样做:

function verifySession() {
var xmlhttp = new XMLHttpRequest();

var returnValue = xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        var response = xmlhttp.responseText;
        console.log(response);                     //<--line 11
        if (response != "verified") {
            window.location.href = "signin.html";
            return false;
        } else {
           return true;
        }
    }
}
xmlhttp.open("GET", "scripts/verifySession.php", true);
xmlhttp.send();
return returnValue;

}

我希望这有帮助!:)

您无法通过这种方式实现目标。在此代码中:

function deleteUser() {
            //check for excessive inactivity
            if (verifySession()) {
                console.log("verified");
            } else {
                console.log("verification failed");  //<--line 33
                return false;
            }
            //continue on...

您使用verifySession()的条件将始终计算为false,因为此函数本身不会返回任何值。尽管您认为您从onreadystatechange函数返回了一个值,但实际上并非如此。初始函数不会等待它,因为您将它作为异步请求发送。尝试使用以下内容:

xmlhttp.open("GET", "scripts/verifySession.php", false);

但我必须警告您,使用同步的ajax请求并不是一个好主意。它可能会冻结浏览器之类的东西。。

这在javascript中是不可能的,因为它不等待ajax请求返回值,您所能做的就是这样。。。

删除用户()

function deleteUser() {
  var callback  = function() {
    if ( this.readyState === 4 && this.status === 200 ) {
      var response  = this.responseText;
      console.log( response );
      if ( response !== "verified" ) {
        window.location.href  = "signin.html";
        console.log( "verification failed" );
      }
      else {
        console.log( "verified" );
        //continue on...
        // here all your other codes from 'deleteUser()'
      }
    }
  };
  verifySession( callback );
  return false;
}

verifySession()

function verifySession( callback ) {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange  = callback;
  xmlhttp.open( "GET", "scripts/verifySession.php", true );
  xmlhttp.send();
}