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

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>

二、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; 
    }  
  }
}

三、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;>

希望本文所述实例对大家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>
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.