php ajax检测用户名是否存在

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