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?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页面代码如下:
<?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 "用户名可以使用"; } } ?>
希望本文所述实例对大家PHP程序开发有所帮助。
文件包括:
userreg.html ( 注册页面)
ajaxreg .js(AJAX脚本及实时验证的JS脚本)
checkuserreg .php(连接数据库并检测用户名是否已注册的页面)
userreg.html ( 注册页面) 复制PHP内容到剪贴板
PHP代码:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Notepad++7.3.1
Easy-to-use and free code editor