check_user.phpjavascript
<?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.phpphp
<!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.jshtml
$(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");
});
});