使用mysql在php中通过AJAX实时检查用户名是否存在 [英] check if username exists in real-time through AJAX in php with mysql

查看:17
本文介绍了使用mysql在php中通过AJAX实时检查用户名是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 php/MySQL 中开展一个项目,该项目要求我在用户输入用户名时实时检查用户名.

这是我的 username.php,用户实际输入用户名 &密码以及从哪里触发 check.php...

I am working on a project in php/MySQL that requires me to check the username in real-time means as the user inputs the username.

This is my username.php where the user actually enters the username & password and from where the check.php is triggered...

<html>
<head>
   <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>

   <script type="text/javascript">

     $(document).ready(function(){
        $("#username").change(function(){
             $("#message").html("<img src='images/loader.gif' /> checking...");


        var username = $("#username").val();

          $.ajax({
                type:"post",
                url:"check.php",
                data:"username =" + username,
                    success:function(data){
                    if(data==0){
                        $("#message").html("<img src='images/tick.png' /><span style='font-size:13px; color: black'> Username available</span>");
                    }
                    else{
                        $("#message").html("<img src='images/err.png' /><span style=font-size:13px; color: red'> Username already taken</span>");
                    }
                }
             });

        });

     });

   </script>
   </head>

   <body>

   <table>
    <tr>
          <td>Username</td>
          <td>:</td>
          <td><input type="text" name="username" id="username"/><td>
            <td id="message"><td>
    </tr>

    <tr>
          <td>Password</td>
          <td>:</td>
          <td><input type="text" name="password" id="password" /><td>
    </tr>
   </table>
   </body>
   </html>

这是在数据库中检查用户名的 check.php.

This is the check.php where the username is checked in the database.

<?php

$con = mysqli_connect("localhost", "root", "hilbi", "userdb");

if (mysqli_connect_errno())
{

  echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

  // $username = $_POST["username"];

  $username = 'hilbi';

  $query = mysqli_query($con,"SELECT * FROM users WHERE username =   '$username' ");

  $find = mysqli_num_rows($query);

  echo $find;

  mysqli_close($con);

  ?>

现在 check.php 工作正常(我通过单独执行检查它并在找到用户名时返回 1反之亦然>)
但是,当我执行username.php它总是返回用户名已经被占用.似乎它实际上根本没有在数据库中查找实际上没有这样的用户名.

任何形式的帮助将不胜感激...

Now the check.php is working fine (which I checked through executing it alone and it returns 1 on finding the username and vice versa)
However when I execute the username.php it always returns Username already taken. It seems it actually does not look in the database at all where actually there is no such username.

Any kind of help will be much appreciated...

推荐答案

像这样使用 jQuery 在 javascript 中发送数据:

Send your data in javascript using jQuery like this:

$.post( "check.php", { user: $("#username").val() }, function (data){
  if(data=='1'){
   //do 1
  }
  elseif(data=='0'){
    //do 0
  }
});

在你的 check.php 中获取这样的用户名

In your check.php get username like this

//some basic validation
if(!isset($_POST['user'] || empty($_POST['user']))
{
   echo '0'; exit();
}

$username = trim($_POST['user']); 

为了使其正常工作,您 check.php 必须返回 1 或 0,不要在那里或任何地方回显 mysql 错误.如果出现任何错误,就回显 0.

For this to work properly you check.php must return either 1 or 0, do not echo mysql errors there or whatsoever. if any error occur, just echo 0.

这篇关于使用mysql在php中通过AJAX实时检查用户名是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆