且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用mysql在php中通过AJAX实时检查用户名是否存在

更新时间:2023-11-28 13:52:58

像这样使用 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.