本程式包括三個檔案
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="網頁特效"> 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); if($_get['act'] == 'reg'){ if(!empty($row['username'])){ //只要註冊使用者名稱為kaixin的時候,註冊頁面就會用紅色字型提示次使用者已被註冊! echo 'no'; }else{ echo 'yes'; } } } ?> |
conn.php資料庫檔案
| 代碼如下 |
複製代碼 |
<?php /* created on 下午12:08:25*/ $conn=@mysql_connect("localhost","root","")or die("111cn.net提示你:串連失敗!"); 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', '111cn.net'), (3, 'xiaot', 'xiaot'), (4, 'xiaoe', 'xiaoe'), (5, '我愛www.111cn.net', '5201314110'); |