如何从调用的onreadystatechange =函数的函数的返回值() [英] how to return variable from the function called by onreadystatechange=function()

查看:1630
本文介绍了如何从调用的onreadystatechange =函数的函数的返回值()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是与PHP结合AJAX技术,我想知道如何从被称为onreadstatechange功能得到返回变量。

I'm using the ajax technique in combination with php and I want to know how to get the return variable from the function that is called by onreadstatechange.

Java函数被onsubmit的调用,然后调用PHP脚本来验证一些事情在我的数据库,并返回true或false取决于结果。

A java function is called onsubmit to then call a php script to verify some things in my database and return true or false depending on the results.

下面是我遇到的问题,我想的onsubmit =返回snappyajaxfunction()的基础上从PHP的结果返回或真或假。

Here is where I run into problems, I want the onsubmit="return snappyajaxfunction()" to return false or true based on the results from the php.

我的问题是,如何获取的或真或假的stateChanged函数的结果成为回报snappyajaxfunction。

My question is, how do I get the result of false or true from the stateChanged function to become the return for snappyajaxfunction.

我试图做这样踢:结果= request.onreadystatechange = StateChanged没有运气

I tried doing this for kicks: result = request.onreadystatechange=StateChanged with no luck.

我也试着保存responsetxt到一个全局变量,然后在snappyajaxfunction没有运气返回全局变量。

I've also tried saving the responsetxt to a global variable then returning the global variable in snappyajaxfunction with no luck.

snappyajaxfunction()
{

   request.onreadystatechange=stateChanged;     	
   request.open("GET",url,true); 
   request.send(null);

   return result;
} 

function stateChanged() 
{ 
    if (request.readyState==4)
    {   
       return request.responseText;	
    }   	
}

这样做的目的是为了脚本是验证通过与存储在我的数据库中的用户名/密码的用户输入的用户名/密码。如果它们不匹配返回false和窗体不会提交,如果这样做的表单提交,用户被带到欢迎页面...

The purpose of this is to script is to verify the username / password entered by the user with the username / password stored in my database. If it they don't match it returns false and the form will not submit, if it does the form submits and the user is brought to the welcome page...

推荐答案

您可以这样做:

function stateChanged() 
{ 
    if (request.readyState==4)
    {   
       OnStateChanged(request.responseText);
    }           
}

其中, OnStateChanged 的是,作为一个事件处理函数...

Where OnStateChanged is a function that act as an event handler...

编辑: OnStateChanged 则可以基于响应提交与否表单

in OnStateChanged you can then submit or not your form based on response

EDIT2:在当您检查的U / P,如果他们是正确的登录用户权限上的状态返回重新提交表单的JavaScript的..然后在JavaScript中,而不是,只是重装/重定向网页服务器如果它成功或以其他方式显示一个警告...

On the server when you check the u/p, if they're right log the user right on and return the status to JavaScript.. then in the JavaScript instead of resubmitting a form, just reload/redirect the page if it is successful or display an alert otherwise...

在这异步操作从函数返回的东西是不可能的......执行原来的线程早已逝去。异步操作被另一个线程执行的(如果我没记错的话,不熄火)。

It is just not possible to return something from a function by an asynchronous operation in it... the original thread of execution is long gone. The asynchronous operation is executed by another thread (If I remember correctly, don't flame).

感谢

这篇关于如何从调用的onreadystatechange =函数的函数的返回值()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆