jquery json 判断用户是否已注册

check_user.php

<?php 
	$link=mysqli_connect('localhost','root','root','test');//连接数据库
	if(mysqli_connect_errno()){
        echo mysqli_connect_error();
	}
 
            $query="select * from username where username='{$_GET['username']}'";
            $result=mysqli_query($link,$query);
            $data=mysqli_fetch_assoc($result);

            echo $data=json_encode($data);
		
?>

 index.php

<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="js/index.js"></script>
</head>
<body>
	<input type="text" id="namevalue"><span class="error"></span>
	<br>
	<button id="btn">Send</button>
	结果:<span id="result"></span>
</body>
</html>

index.js

$(function(){
    $("#namevalue").blur(function(){
        $.get("/jquery/check_user.php",{
            username:$("#namevalue").val()
        },function(data,status){
            if(data!=null){
                $("#result").text("");
                $(".error").text("用户名已注册");
            }else{
                $(".error").text("");
                $("#result").text($("#namevalue").val());
            }
    },"json");
    });
});
原文地址:https://www.cnblogs.com/zgl-x/p/6096758.html