[第三弹]利用ajax技术,验证用户名是否被占用

前端之家收集整理的这篇文章主要介绍了[第三弹]利用ajax技术,验证用户名是否被占用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

demo01.html

<html>
	<head>
		<script src="public.js"></script>
		<script>
			window.onload = function(){
				$('username').onblur = function(){
					var username = $('username').value;
					//var passwd = $('passwd').value;
					var xmlHttp = createXMLHttp();
					var url = 'demo01.PHP?username=' + username + '&_=' + new Date().getTime();
					xmlHttp.open('get',url);
					xmlHttp.onreadystatechange = function(){
						if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
							var flag = xmlHttp.responseText;
							if(flag == 1){
								//flag = 1表示用户名占用
								$('display').innerHTML = "<font color=red>用户名被占用,请更换!</font>";
							}
							else{
								$('display').innerHTML = "<font color=green>用户名可用</font>";
							}
						}
					};
					xmlHttp.send(null);
				};
			};
		</script>
	</head>
	
	<body>
		<form method="psot" action="demo04.PHP">
			用户名:<input type="text" id="username" name="username"> <span id="display" ></span>
			<br />
			密码:<input type="password" id="passwd" name="passwd">
			<br />
			<input type="submit" value="注册">
			<input type="hidden" name="act" value="register">
		</form>
	</body>
</html>

demo01.PHP
<?PHP

$username = $_GET['username'];
$link = MysqL_connect('127.0.0.1:3306','root','passwd');
MysqL_select_db('test');
MysqL_query('set names utf8');
$result = MysqL_query("select * from admin where username='$username'");
$n = MysqL_num_rows($result);
// $row = MysqL_fetch_row($result);
MysqL_close($link);
if($n > 0){
	echo 1;
}
else{
	echo 0;
}

猜你在找的Ajax相关文章