Home > php教程 > php手册 > body text

PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程,ajax实例教程

WBOY
Release: 2016-06-13 09:26:37
Original
1259 people have browsed it

PHP+Ajax检测用户名或邮件注册时是否已经存在实例教程,ajax实例教程

PHP+Ajax检测用户名或邮件注册时是否已经存在是论坛或会员系统中常见的一个重要功能。本文就以实例形式简单描述这一功能的实现方法。具体步骤如下:

一、PHP检测页面

check.php页面代码如下:

<script type="text/javascript" src="jiance.js"></script>
<form name="myform" action="" method="get">
 用户名:<input name="user" value="" type="text" onblur="funtest100()" />
 <div id="test100"></div>
</form>

Copy after login

二、Ajax验证页面

check.js页面代码如下:

var xmlHttp;
function S_xmlhttprequest(){
  if(window.ActiveXobject){
    xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
  }else if(window.XMLHttpRequest){
    xmlHttp = new XMLHttpRequest();
  }
}
function funtest100(){
  var f = document.getElementsByTagName_r('form')[0].user.value;//获取文本框内容
  S_xmlhttprequest();
  xmlHttp.open("GET","jcfor.php&#63;id="+f,true);//找开请求
  xmlHttp.onreadystatechange = byphp;//准备就绪执行
  xmlHttp.send(null);//发送
}
function byphp(){
  //判断状态
  if(xmlHttp.readyState==1){//Ajax状态
    document.getElementByIdx_x_x('test100').innerHTML = "正在加载";
  }
  if(xmlHttp.readyState==4){//Ajax状态
    if(xmlHttp.status==200){//服务器端状态
      var bytest100 = xmlHttp.responseText;
      //alert(bytest100);
      document.getElementByIdx_x_x('test100').innerHTML = bytest100; 
    }  
  }
}

Copy after login

三、PHP验证页面

chkfor.php页面代码如下:

<&#63;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 "用户名可以使用"; 
    }
 }  
&#63;>

Copy after login

希望本文所述实例对大家PHP程序开发有所帮助。

那个朋友有PHP+AJAX检验用户名的代码

文件包括:

userreg.html ( 注册页面)
ajaxreg .js(AJAX脚本及实时验证的JS脚本)
checkuserreg .php(连接数据库并检测用户名是否已注册的页面)
userreg.html ( 注册页面) 复制PHP内容到剪贴板
PHP代码:



无标题文档 <br><script language="javascript" src="ajaxreg.js"></script> <br /><script language="JavaScript" type="text/JavaScript"> <br />function check(){ //用户名为空的时候 <br />if(document.reg.username.value==""){ <br />document.getElementById('check').innerHTML=" <font color=red>用户名不能为空!</font>"; <br />document.reg.username.focus(); <br />return false; <br />} <br />if(document.getElementById('check').innerHTML==" <font color=red>The number is registed</font>"){ //用户名已被注册的时候(<font color=red>The number is registed</font>是AJAX返回回来的) <br />document.reg.username.focus(); <br />return false; <br />} <br />if(document.reg.userpwd.value==""){ //密码为空的时候 <br />document.getElementById('pwd').innerHTML=" <font color=red>用户密码不能为空!</font>"; <br />document.reg.userpwd.focus(); <br />return false; <br />} <br />if(document.reg.userpwd.value.length<6){ //密码长度错误的时候 <br />document.getElementById('pwd').innerHTML=" <font color=red>密码长度不能小于6位!</font>"; <br />document.reg.userpwd.focus(); <br />return false; <br />} <br /><br />if (document.reg.reuser......余下全文>><br/>   <p class="header2"><span class="icon i-relatedanswer"><h3>javascript ajax php结合查询注册用户名是否已经存在问题<p class="best-replyer"> <p class="ft"> <script type='text/javascript'><br />var username=$('#username').val();<br />$.post('xx.php',{username:username},function(data){<br /> if(data==1){<br /> alert("已存在");//或者 让个span或者div 显示 $('#xx').html(“已存在”);<br />}<br />});<br /></script><br />xx.php <br />$username=$_POST['username'];<br />$sql="select * from user where username=$username";<br />$handle=mysql_query($sql);<br />$num=mysql_num_rows($handle);<br />if($num>0){<br /> $flag=1;<br />}else{<br />$flag=0;<br />}<br />exit($flag);<br/>   </script>
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!