<p>PHP+Ajax检测用户名或邮件注册时是否已经存在是论坛或会员系统中常见的一个重要功能。本文就以实例形式简单描述这一功能的实现方法。具体步骤如下:</p><p>一、PHP检测页面</p><p>check.php页面代码如下:</p><pre class="brush:html;toolbar:false"><scripttype="text/javascript"src="jiance.js"></script>
<formname="myform"action=""method="get">
用户名:<inputname="user"value=""type="text"onblur="funtest100()"/>
<divid="test100"></div>
</form></pre><p>二、Ajax验证页面</p><p>check.js页面代码如下:</p><pre class="brush:js;toolbar:false">varxmlHttp;
functionS_xmlhttprequest(){
if(window.ActiveXobject){
xmlHttp=newActiveXObject('Microsoft.XMLHTTP');
}elseif(window.XMLHttpRequest){
xmlHttp=newXMLHttpRequest();
}
}
functionfuntest100(){
varf=document.getElementsByTagName_r('form')[0].user.value;//获取文本框内容
S_xmlhttprequest();
xmlHttp.open("GET","jcfor.php?id="+f,true);//找开请求
xmlHttp.onreadystatechange=byphp;//准备就绪执行
xmlHttp.send(null);//发送
}
functionbyphp(){
//判断状态
if(xmlHttp.readyState==1){//Ajax状态
document.getElementByIdx_x_x('test100').innerHTML="正在加载";
}
if(xmlHttp.readyState==4){//Ajax状态
if(xmlHttp.status==200){//服务器端状态
varbytest100=xmlHttp.responseText;
//alert(bytest100);
document.getElementByIdx_x_x('test100').innerHTML=bytest100;
}
}
}</pre><p>三、PHP验证页面</p><p>chkfor.php页面代码如下:</p><pre class="brush:php;toolbar:false"><?php
if($_GET[id]){
sleep(1);
$conn=mysql_connect('localhost','root','');
mysql_select_db('test',$conn);
$sql="SELECT*FROM`user`WHERE`name`='$_GET[id]'";
$q=mysql_query($sql);
if(is_array(mysql_fetch_row($q))){
echo"用户名已经存在";
}else{
echo"用户名可以使用";
}
}
?></pre><p>希望本文所述实例对大家PHP程序开发有所帮助。</p>