weixin_33737774 2011-11-05 10:36 采纳率: 0%
浏览 24

如何使$ .get返回一个值[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/7779697/javascript-asynchronous-return-value-assignment-with-jquery" dir="ltr">JavaScript asynchronous return value / assignment with jQuery [duplicate]</a>
                            <span class="question-originals-answer-count">
                                (2 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2014-03-13 00:45:29Z" class="relativetime">6 years ago</span>.</div>
        </div>
    </aside>

I have this code in JavaScript and jQuery and I need to fix it:

function valid_form() {
val_avail_email=valid_avail_email();

if (val_avail_email!="") {
$("#email_valid").html(val_avail_email);
return false;
}

return true;
}

function valid_avail_email() {
    var error="";

    $.get("valid_avail_email.php",{email:document.reg_form.email_reg.value},function(output) {
        error=output;
    });

    return error;
}

So obviously I want the error variable to contain the output of the valid_avail_email.php and than make the valid_avail_email() function return that value.

Here is the code in valid_avail_email.php:

<?php
include("config.php");

$email=$_GET["email"];

$sql="select email from users";
$sql_query=mysql_query($sql);

if (!$sql_query) {
    echo mysql_error();
}

$res_assoc = mysql_fetch_assoc($sql_query);

foreach ($res_assoc as $field=>$value) {
   if ($value==$email) {
       echo "please use another email this one is taken ";
   }
}

?>  
</div>
  • 写回答

2条回答 默认 最新

  • weixin_33736832 2011-11-05 10:46
    关注

    Problem is that the $.get() function initiates an asynchronous ajax call, means, the function returns while the ajax call is still running. You have to inform the GUI from within the ajax callback function, e.g.:

    $.get("valid_avail_email.php",{email:document.reg_form.email_reg.value},function(output) {
      error=output;
      if (error !== '')
        alert('Sorry, use another email!');
    

    });

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮