用ajax jquery php mysql检查用户名

时间:2022-09-21 16:53:35

I have table users :

我有桌面用户:

  • id : int,
  • id:int,
  • username : varchar(255),
  • 用户名:varchar(255),
  • password :varchar(255),
  • 密码:varchar(255),

I would check if the login exist in the database or not

我会检查数据库中是否存在登录名

I try with this code jquery and html :

我尝试使用此代码jquery和html:

 <div class="form-group">
     <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">username <span class="required">*</span></label>
     <div class="col-md-6 col-sm-6 col-xs-12">
        <input type="text"  class="form-control col-md-7 col-xs-12"  name="username " id="username ">
          <span id="availablity"></span>
     </div>
 </div>
<script type="text/javascript">

    $('document').ready(function(){
            $('#username').blur(function(){
                var username = $(this).val();
                $.ajax ({
                    url = "check.php",
                    method = "POST",
                    data =  {username :username },
                    dataType : "text",
                    success:function(html)
                    {
                        $('#availablity').html(html);
                    }
                });
            });
    });
</script>

page check.php :

page check.php:

if(isset($_POST['username'])){
                $username  = $_POST["username"];
                $sql = $db->query("SELECT * FROM users WHERE username  = '$username'");
                if($sql->rowCount() > 0){
                     echo '<span class="text-danger">Not Exist</span>';
                }else{
                    echo '<span class="text-success">Exist</span>';
                }

}

But no results

但没有结果

4 个解决方案

#1


1  

Please try following code.

请尝试以下代码。

JavaScript code

JavaScript代码

 $('document').ready(function(){
          $('#username').change(function(){
               var username = $(this).val();
                $.ajax ({
                    url : "check.php",
                    method : "POST",
                    data :  {username :username },
                    dataType: "text",
                    success:function(html)
                    {
                        $('#availablity').html(html);
                    }
                });
            });
   });

And you've mentioned id="username " (with space), please change that like as below.

你已经提到id =“username”(带空格),请改变如下。

<input type="text"  class="form-control col-md-7 col-xs-12"  name="username" id="username">

#2


1  

You have used = in your ajax configuration. Thats an syntax error. Use

您在ajax配置中使用了=。这是一个语法错误。使用

$.ajax({
    url: "check.php",
    method: "POST",
    data: {username: username},
    dataType: "text",
    success: function (html) {
        $('#availablity').html(html);
    }
});

instead.

代替。

#3


0  

This should be your code:

这应该是你的代码:

$('document').ready(function(){
            $('#username').blur(function(){

                var username = $(this).val();
                $.ajax ({
                    url :"check.php",
                    method: "POST",
                    data : {username :username },
                    dataType : "text",
                    success:function(html)
                    {
                        $('#check').html(html);
                    }
                });
            });
    });

#4


0  

looks Jquery syntax error ...

看起来Jquery语法错误...

<script type="text/javascript">

    $('document').ready(function(){
            $('#username').blur(function(){
                var username = $(this).val();
                $.ajax ({
                    url : "check.php",
                    method : "POST",
                    data :  {username :username },
                    dataType : "text",
                    success:function(html)
                    {
                        console.log(html);
                        $('#availablity').html(html);

                    },
                    error: function(html)
                    {
                        console.log(html);
                    }
                });
            });
    });
</script>

check your console message.

检查你的控制台消息。

#1


1  

Please try following code.

请尝试以下代码。

JavaScript code

JavaScript代码

 $('document').ready(function(){
          $('#username').change(function(){
               var username = $(this).val();
                $.ajax ({
                    url : "check.php",
                    method : "POST",
                    data :  {username :username },
                    dataType: "text",
                    success:function(html)
                    {
                        $('#availablity').html(html);
                    }
                });
            });
   });

And you've mentioned id="username " (with space), please change that like as below.

你已经提到id =“username”(带空格),请改变如下。

<input type="text"  class="form-control col-md-7 col-xs-12"  name="username" id="username">

#2


1  

You have used = in your ajax configuration. Thats an syntax error. Use

您在ajax配置中使用了=。这是一个语法错误。使用

$.ajax({
    url: "check.php",
    method: "POST",
    data: {username: username},
    dataType: "text",
    success: function (html) {
        $('#availablity').html(html);
    }
});

instead.

代替。

#3


0  

This should be your code:

这应该是你的代码:

$('document').ready(function(){
            $('#username').blur(function(){

                var username = $(this).val();
                $.ajax ({
                    url :"check.php",
                    method: "POST",
                    data : {username :username },
                    dataType : "text",
                    success:function(html)
                    {
                        $('#check').html(html);
                    }
                });
            });
    });

#4


0  

looks Jquery syntax error ...

看起来Jquery语法错误...

<script type="text/javascript">

    $('document').ready(function(){
            $('#username').blur(function(){
                var username = $(this).val();
                $.ajax ({
                    url : "check.php",
                    method : "POST",
                    data :  {username :username },
                    dataType : "text",
                    success:function(html)
                    {
                        console.log(html);
                        $('#availablity').html(html);

                    },
                    error: function(html)
                    {
                        console.log(html);
                    }
                });
            });
    });
</script>

check your console message.

检查你的控制台消息。