简体   繁体   English

Ajax请求检查用户名

[英]Ajax request to check username

I am creating a signup form in HTML/CSS/JS which uses an AJAX request to get response from server. 我在HTML / CSS / JS中创建一个注册表单,它使用AJAX请求从服务器获取响应。 In my jQuery, I use a method to validate form contents which also calls a function (containing ajax) to see if the username exists or not. 在我的jQuery中,我使用一种方法来验证表单内容,它还调用一个函数(包含ajax)来查看用户名是否存在。 I have checked the similar questions but couldn't relate to my problem. 我检查了类似的问题,但无法解决我的问题。

The AJAX goes inside a function like this AJAX进入这样的函数

function checkIfUserNameAlreadyExists(username)
{
    // false means ok, i.e. no similar uname exists
    $.ajax
    ({
        url  : 'validateUsername.php',
        type : 'POST',
        data : {username:username},

        success : function(data,status)
        {
            return data;
        } 

    });
}

The PHP code looks like this PHP代码看起来像这样

<?php 

if($_SERVER['REQUEST_METHOD']=='POST')
{
    $enteredLoop=false;
    $linkobj = new mysqli('localhost','root','','alumni');
    $query = "select username from user where username='".$uname."'";
    $stmt = $linkobj->prepare($query);
    $stmt->execute();
    $stmt->bind_result($uname);
    while($stmt->fetch())
        $enteredLoop=true;

    if($enteredLoop)
    {
        echo "
        <script type='text/javascript'>
        $('.unamestar').html('Sorry username already exists');
        $('.userName').css('background-color','rgb(246, 71, 71)');
                $('html,body').animate({
                    scrollTop: $('.userName').offset().top},
                    'slow');
        </script>";
        return;
    }
}
?>

The function checkIfUserNameAlreadyExists returns false by default (don't know how) or this ajax request is not submitted, and it submits the form details to php. 函数checkIfUserNameAlreadyExists默认返回false (不知道如何)或者没有提交此ajax请求,并将表单详细信息提交给php。

Any help ? 有帮助吗?

Your checkIfUserNameAlreadyExists() function is synchronous and your ajax call is asynchronous. 您的checkIfUserNameAlreadyExists()函数是同步的,您的ajax调用是异步的。 That means that your function will return a value (actually no value is returned at all in your case...) before the ajax call is finished. 这意味着在ajax调用完成之前,您的函数将返回一个值(实际上在您的情况下根本没有返回任何值...)。

The easiest way to solve this, is to generate the html in the success function, based on the return value of the data variable. 解决此问题的最简单方法是根据data变量的返回值在success函数中生成html。

Something like: 就像是:

    success : function(data,status) {
        if (data === 'some_error') {
          // display your error message, set classes, etc.
        } else {
          // do something else?
        }
    } 

Apart from that, are you actually setting the value of $uname to $_POST['username'] ? 除此之外,你实际上是将$uname的值设置为$_POST['username']吗?

You need to append response script to the document for executing. 您需要将响应脚本附加到文档以便执行。

function checkIfUserNameAlreadyExists(username)
    {
        // false means ok, i.e. no similar uname exists
        $.ajax
        ({
            url  : 'validateUsername.php',
            type : 'POST',
            data : {username:username},        
            success : function(response)
            {
                $('body').append(response);
            } 

        });
    }

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

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