本文介绍了jQuery到php(等待php脚本,直到结果/完成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从javascript(jQuery)执行php调用,php sript将结果发送回去.问题在于这些php脚本要花费一些时间(毫秒),而Java脚本没有等待它们完成,因此变量未使用正确的值初始化.代码很简单:

I am executing a php call from javascript(jQuery), php sript is sending result back. The problem is that these php scripts are taking some time (milliseconds), and java script is not waiting for them to finish, thus variables are not getting initialized with correct values.The code is simple:

$.get("php/validation.php",{'email':email},function(data){
// valid_email now contains true/false
alert(data);
if(data=="true"){
var valid_email = true;}
});

警报"打印为true,但在下面的代码中将valid_mail的值识别为假".还有其他更好的方法来调用php脚本并等待直到脚本还没完成吗?

The "alert" is printing true but value of valid_mail is recognized as "false" in the code below. Is there any other better way to call php scripts and wait for until they are not finished?

农药

推荐答案

function(data)被异步调用;它一直等到PHP脚本完成之后.但是,该$.get()块以下的任何代码都将立即执行.如果您还希望下面的代码等待,则必须将其上移到该函数中,或将其放入新函数中,然后调用它.

That function(data) is called asynchronously; it waits until after the PHP script has finished. Any code below that $.get() chunk, however, is executed immediately. If you want the code below to wait as well, you have to move it up into that function, or put it into a new function and just call it.

这篇关于jQuery到php(等待php脚本,直到结果/完成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:40