php+ajax用户注册验证用户是否在存
发布:smiling 来源: PHP粉丝网 添加日期:2014-09-11 23:07:27 浏览: 评论:0
这是一个完整理的php+mysql+ajax的用户注册实例程序,可以提供检测用户名是否被注册,这样可以增强用户体验,在你填写好用户名后就会提供你当前所有注册的用户是否己经被注册了.
本程序包括三个文件:
reg.html 用户注册html页面
reg.php php处理代码
conn.php 数据库教程连接文件
reg.html代码如下:
- <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=gb2312" />
- <title>php+ajax用户注册验证用户是否在存(php mysql完整实例)</title>
- <style type="text/css">
- body{
- font-size:12px;
- text-align:center;
- }
- .text{
- width:180px;
- height:12px;
- }
- p{
- width:600px;
- height:20px;
- line-height:20px;
- text-align:left;
- }
- p label{
- display:block;
- width:80px;
- height:20px;
- line-height:20px;
- float:left;
- text-align:right;
- }
- p span{
- margin-left:10px;
- }
- </style>
- </head>
- <body>
- <script language="javascript">
- function createxmlhttprequest(){
- var xmlhttp;
- if(window.activexobject){
- xmlhttp = new activexobject("microsoft.xmlhttp");
- }else if(window.xmlhttprequest){
- xmlhttp = new xmlhttprequest();
- }
- return xmlhttp;
- }
- function checkname(){
- var name = document.getelementbyid('name'); //获取用户名文本框
- var span = document.getelementbyid('name_info'); //获取用于显示结果的span标记
- if(name.value.length <= 4){
- span.style.color = '#ff0000'; //设置span标记内的字体颜色为红色
- span.innerhtml = '用户名长度不能少于4个字符!'; //span标记内容
- return false;
- }
- var xmlhttp = createxmlhttprequest();//创建异步请求对象
- var time = new date().gettime();
- var url = 'reg.php?act=reg&name=' + name.value.tolowercase() + '&tmp=' + time;//构造出请求地址
- xmlhttp.open("get",url,true); //建立一个异步请求
- /*这里我们使用get方式请求
- post方式的请求基本差不多,朋友们自己试试如果不行,在下面给我留言*/
- xmlhttp.onreadystatechange = function(){ //监视请求状态
- span.style.color = '#ff9900';
- span.innerhtml = '查询中,请稍候!';
- if(xmlhttp.readystate == 4 && xmlhttp.status == 200){
- if(xmlhttp.responsetext.indexof('no') != -1){ //如果服务器返回的信息中有no
- span.style.color = '#cb2121'; //设置span标记颜色为红色
- span.innerhtml = '用户名[' + name.value + ']已经被别的用户使用!';
- }else{//如果返回信息中没有no
- span.style.color = '#00a800';//设置颜色为绿色
- span.innerhtml = '恭喜您,该用户名未被注册!';
- }
- return true;
- delete xmlhttp; //删除请求对象
- }
- }
- xmlhttp.send(null); //发送请求
- }
- </script>
- <form method="post" action="reg.php">
- <p><label>用户名:</label><input type="text" class="text" id="name" name="user_name"/><span id="name_info"></span></p>
- <p><label></label><input type="button" value="检查用户名" onclick="checkname()"/></p>
- <p><label>密码:</label><input type="password" class="text" /></p>
- <p><label> </label><input type="submit" value="注册" /></p>
- </form>
- </body>
- </html>
reg.php文件,代码如下:
- <?php
- include("conn.php");
- $name=$_get['name'];
- $name=urldecode($name);
- if(strlen($name)>0){
- $sql="select username from registration where username = '$name'";
- $result=mysql_query($sql);
- $row=mysql_fetch_array($result);
- //开源代码phpfensi.com
- if($_get['act'] == 'reg'){
- if(!emptyempty($row['username'])){ //只要注册用户名为kaixin的时候,注册页面就会用红色字体提示次用户已被注册!
- echo 'no';
- }else{
- echo 'yes';
- }
- }
- }
- ?>
conn.php数据库文件,代码如下:
- <?php
- /* created on 下午12:08:25*/
- $conn=@mysql_connect("localhost","root","")or die("phpfensi.com提示你:连接失败!");
- mysql_select_db("reg",$conn);
- mysql_query("set names 'gbk'");
- ?>
registration数据表结构,代码如下:
- -- phpmyadmin sql dump
- -- version 2.11.2.1
- -- http://www.phpmyadmin.net
- --
- -- 主机: localhost
- -- 生成日期: 2009 年 05 月 20 日 05:29
- -- 服务器版本: 5.0.45
- -- php 版本: 5.2.5
- set sql_mode="no_auto_value_on_zero";
- --
- -- 数据库: `reg`
- --
- -- --------------------------------------------------------
- --
- -- 表的结构 `registration` phpmyadmin导入数据
- --
- create table `registration` (
- `id` tinyint(6) not null auto_increment,
- `username` varchar(14) not null comment '注册用户名',
- `userpwd` varchar(14) not null comment '注册密码',
- primary key (`id`)
- ) engine=innodb default charset=gb2312 auto_increment=6 ;
- --
- -- 导出表中的数据 `registration`
- --
- insert into `registration` (`id`, `username`, `userpwd`) values
- (1, 'admin', 'admin888'),
- (2, 'lyn', 'phpfensi.com'),
- (3, 'xiaot', 'xiaot'),
- (4, 'xiaoe', 'xiaoe'),
- (5, '我爱www.phpfensi.com', '5201314110');
Tags: php+ajax用户注册 php注册验证
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)