简体   繁体   English

Ajax调用太慢了? Javascript和PHP

[英]Ajax call too slow ? Javascript and PHP

I am setting up a registration form. 我正在建立一个登记表。 This form has 7 fields that are being tested for validation. 此表单有7个字段正在测试以进行验证。 2 of them have a special validation; 其中2个有特殊验证; I have an Ajax call to a PHP class where I send an email_string to this class, testing, if such an email already exists in my database (evading duplicates). 我有一个对PHP类的Ajax调用,我向这个类发送一个email_string,测试,如果我的数据库中已存在这样的电子邮件(避免重复)。

params = {};
params['source'] = 'checkEMail';
params['email']  = email
$.ajax({
    type: 'POST',
    url : 'some_class.php',
    data : params,
    success: function(msg)
    {
        if(msg > 0) /* email exists */
            is_error = true;
    }
});

And in my PHP Class I got something like this: 在我的PHP类中我得到了这样的东西:

$final = mysql_fetch_assoc(foo);
echo ($final) ? 1 : 0;

I was testing the data and in fact - I get '1' if the email exists and '0' if it does not. 我正在测试数据,实际上 - 如果电子邮件存在,我得到'1',如果没有,我得'0'。

Funny thing is, that this snippet works fine AS LONG AS there are other errors in this form - like an empty username. 有趣的是,这个段工作正常,只要有这种形式其他错误-像一个空的用户名。 The "is_error" is a boolean that is set to false at the beginning of the script and if one validation fails, it gets true. “is_error”是一个布尔值,在脚本开头设置为false,如果一个验证失败,则为true。 Then, finally, I got this: 然后,最后,我得到了这个:

if(is_error)
{
    error.show();
    error.empty().append('some html foo');
}
else
{
    $('form').submit();
    error.empty().hide();
}

So how can it be that I the form will be send although is_error is set to true? 那么即使is_error设置为true,我怎么能发送表单呢? Only reason I could think of is that the form is being send before the "is_error" is being testet - but the send of the form is at the very bottom of the script. 我能想到的唯一原因是表单是在“is_error”被测试之前发送的 - 但是表单的发送位于脚本的最底层。

Oh and I am calling it this way: 哦,我这样称呼它:

<script type="text/javascript">
            $("#reg_submit").click(function(event) {
                event.preventDefault(); 
                checkReg();
            });
</script>

Maybe it is caused by different output types that you are trying to match... 也许它是由您尝试匹配的不同输出类型引起的...

First, change your PHP output to 首先,将PHP输出更改为

echo json_encode($final ? true : false);

Then, to work with JSON, I would add this line to ajax calling method, to be sure... 然后,为了使用JSON,我会将此行添加到ajax调用方法,以确保...

$.ajax({
    type: 'POST',
    dataType:'json',
    ...
    success: function(msg)
    {
        if(msg!==true){
            is_error = true;
        }
    }
});

instead of having the if(is_error){ part at the end of the script, I would suggest you to do it in the ajax request completion to avoid race conditions: 而不是在脚本的末尾有if(is_error){部分,我建议你在ajax请求完成时这样做以避免竞争条件:

$.ajax({
    type: 'POST',
    url: 'some_class.php',
    data: params,
    success: function(msg) {
        if (msg > 0) /* email exists */
        is_error = true;
    },
    complete: function() {
        if (is_error) {
            error.show();
            error.empty().append('some html foo');
        } else {
            $('form').submit();
            error.empty().hide();
        }
    }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM