比较简单的,我用jQuery库,可从
www.jquery.com下载.
HTML页面:
........
<script type = "text/javascript" src = "jquery.js"></script>
<script type = "text/javascript">
function checkUsername(){
var username = $("#username").val();
$.get("checkusername.php",{name: username},function(txt){
if(txt == 1){$("#msg").html("<p>此用户名可注册</p>");}
else {$("#msg").html("<p>此用户已经被注册注册!</p>");}
});
}
</script>
........
<input id = "username" type = "text" onchange = "checkUsername()" />
<div id = "msg"></div>
........
checkusername.php的内容:
<?php
$hosting = "localhost";//数据库地址
$username = "root";//数据库用户名
$password = "123456";//数据库密码
$database = "my_db";//数据库名
$table = "users";//表名
$name = $_GET['name'];
$link = mysql_connect($hosting ,$username,$password);
mysql_query("USE $database", $link);
$result = mysql_query("SELECT * FROM $table WHERE username='$name'", $link);
$rows = mysql_num_rows($result);
if($rows >= 1){
echo("0");
}else{
echo("1");
}
?>