简体   繁体   English

jquery php ajax 用户名检查功能

[英]jquery php ajax username check function

So i'm usually pretty good at figuring out these types of problems but this one has got me completely stumped as have been trying to fix for about 6 hours now.所以,我在搞清楚这些类型的问题通常很不错,但这个已经让我彻底难倒一直试图修复约6小时了。

i have a web form which contains a username check using jquery script and a php script.我有一个包含使用jQuery脚本和PHP脚本用户名检查Web表单。 it was working fine when i first wrote the code and now has completely stopped working(no idea why).它工作正常,当我第一次写的代码,现在已经完全停止工作(不知道为什么)。 The php script is a pretty standard mysqli query which returns the numrows and then i either echo 1 or 0 to the page depending on if a result was found. PHP脚本是它返回一个numRows行相当标准的mysqli查询,然后我取决于如果结果发现无论是回声1或0到页。 this script works fine and have tested it independently and echos 1 if no result was found and 0 if a result was found.这个脚本工作正常,并有独立和回声1测试,如果没有结果发现和0,如果结果被发现。

in the jquery script i have a min value check and a no value check which both work fine.在jQuery脚本我有一个最小值检查和没有价值的支票都工作得不错。 Then my ajax call and i receive the result with success:function(data).然后我的ajax调用和我收到的成功结果:功能(数据)。 this also has been tested independently by printing the result(data) on screen and prints a 1 on screen when there is no record and a 0 on screen when a record if found so i know everything is working fine with the sent data and im getting the results back i expect.这也得到了在屏幕上打印结果(数据)的独立测试,并打印屏幕上的1时,有没有记录,并在屏幕上0时,如果发现了一个记录,所以我知道一切正常与发送的数据,并即时得到结果回来我的期望。

so my only thing left is there must be a problem with my if statement and how it is dealing with the returned data as it always skips to the else even when the condition is met.所以我唯一剩下的就是必须有一个问题,我的if语句以及它是如何处理返回的数据,因为它总是跳到别的,即使满足条件。 The only way for the if statement to work is when i set the variable myself and run the script.对于if语句的唯一途径是,当我设置变量自己并运行该脚本。 The long story short is everything works fine except for the final if statement where it always jumps to the else no matter what and says the username is available even when i know its not.长话短说是一切工作正常,除了最后的if语句它总是跳转到别的不管,并说该用户名可用,即使我知道它不是。

Im testing on xampp. IM在XAMPP测试。 Could this be an issue too?难道这是一个问题吗?

Any ideas?有任何想法吗? Thanks in advance!!!提前致谢!!!

Here is the code: javascript下面是代码:JavaScript的

$(document).ready(function(){  


  //the min chars for username  
        var usermin_chars = 6; 

        //result texts  
        var checking_html = 'Checking...';  

//when button is clicked  
    $('#username').blur(function(){  
         var usernameVal = $("#username").val();
        if (usernameVal == '') {
        $("#username_result").html('<span class="error">Please enter a username!</span>');
        $('#username').removeClass();
        $('#username').addClass("form_error");
     }
        //run the character number check  
       else if($('#username').val().length < usermin_chars){  
            //if it's bellow the minimum show characters_error text '  
            $('#username_result').html('<span class="error">Username must contain at least 6 characters!</span>'); 
            $('#username').removeClass();
            $('#username').addClass("form_error"); 
        }else{  
            //get the username  
    var username = $('#username').val();  

    $.ajax({
                type:"post",
                url:"checkUsername.php",
                data:"username="+username,
                    success:function(data){
                    if(data==0){$("#usename_result").html("Username already in use! Please choose another username.");
                    $('#username').removeClass();
                    $('#username').addClass("form_error"); 
                    }
                    else{$("#username_result").html("Username available");
                    $('#username_result').html('<span class="ok"><img src="../images/imgs/available.png" width="20" height="20" margin-left="5" alt="tick"></span>');
                    $('#username').removeClass();
                    $('#username').addClass("form_ok");
                    }
                }
             });

        }  
    });  

});

ok so i found the answer.好的,所以我找到了答案。 as the site is near completion i had gone through all the pages and made sure every page had a title for SEO including the checkUser page.由于网站即将完成,我浏览了所有页面,并确保每个页面都有一个 SEO 标题,包括 checkUser 页面。 the page title was being sent through with the data and confusing the if statement.页面标题与数据一起发送并混淆了 if 语句。 when i was testing the fuction if i placed (data) in a span it only showed the 0 or 1 but looking at it in firebug it showed the response as the 0 or 1 plus the page title.当我测试功能时,如果我将(数据)放在一个跨度中,它只显示 0 或 1,但在萤火虫中查看它时,它会将响应显示为 0 或 1 加上页面标题。 Thanks for those who had a look at it.谢谢看过的人。

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

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