每个网站都有检查用户名的应用,检查邮箱的用户名,这个我们使用AJAX来写,当然也是AJAX来完成 首先我们设计公共链接的数据库文件

<?php
header('content-type:text/html;charset=utf-8');
$link=mysql_connect("localhost","root","dgj99349");
mysql_select_db('test',$link);
mysql_set_charset('utf-8',$link);
session_start();
</pre>

然后是我们的注册页面
<pre lang='php' line='1'>
<?php
include('config.php');
if(isset($_POST['sub'])){
 $sql="INSERT INTO `test`.`user` (`username`, `password`) VALUES ('".$_POST['username']."','".md5($_POST['password'])."')";
	if(mysql_query($sql,$link)){
		$query=mysql_query("select id from user where username='".$_POST['username']."'");
		$rs=mysql_fetch_assoc($query);
		echo $_SESSION['uid']=$rs['id'];
		echo "<script>alert('注册成功');location.href='my.php'</script>";
	}else{
		echo "注册失败";
	}
}
?>
<script language="javascript" src="ajax1.js"></script>

<form name='zz' action="" method="post">
帐号:<input name="username" type="text" onblur="widuuajax()" value=''></br><div id="username"></div>
密码:<input name="password" type="password"></br>
<input type="submit" name="sub" value="提交">
</form>
</pre>
然后是主要的ajax文件的源码
<pre lang='js' line='1'>
var xmlHttp;
function S_xmlhttprequest() {
	if(window.ActiveXObject) {
		xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	} else if(window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function widuuajax() {
	var f=document.zz.username.value;
	S_xmlhttprequest();
	xmlHttp.open("GET","yz.php?username="+f,true);
	xmlHttp.onreadystatechange = widuu;
	xmlHttp.send(null);
}
function widuu() {
  	if(xmlHttp.readyState == 1) {
		 document.getElementById('username').innerHTML = "loading.....";
	}
    	if(xmlHttp.readyState == 4 ){
		if(xmlHttp.status == 200) {
          var ajaxcs =  xmlHttp.responseText;
          document.getElementById('username').innerHTML = ajaxcs;
		}
	}


}
</pre>
最后是异步传输执行的那个文件
<pre lang='php' line='1'>
<?php
if($_GET[username]){
	sleep(1);
 $conn=mysql_connect('localhost','root','dgj99349');
 mysql_select_db('test',$conn);

 $sql="SELECT * FROM `user` where `username`='$_GET[username]'";
 $q=mysql_query($sql);

 if(is_array(mysql_fetch_row($q))){
 	echo "<font color=red>用户名已经存在</font>";
 }else
 {
   echo "<font color=green>可以使用</font>";
 }
}
然后主要我给大家了源码,下载地址http://pan.baidu.com/share/link?shareid=105897&uk=3172762343 欢迎大家转载,转载请注明来自微度网络-网络技术中心http://yun.widuu.com

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信小程序

微信扫一扫体验

立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部