程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> ajax 用戶名驗證 代碼

ajax 用戶名驗證 代碼

編輯:PHP基礎知識
 

yanzheng1.php

<!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>用戶名唯一性驗證</title>

<script type="text/javascript">
var xmlobj;

function CreateXMLHttpRequest(){

try{
xmlobj= new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
xmlobj= new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
try{
xmlobj= new XMLHttpRequest();
}catch(e){}
}
}

}
/*
function CreateXMLHttpRequest(){

if(window.ActiveXObject){ //如果當前浏覽器支持ActiveXobject,則創建ActiveXObject對象
xmlobj = new ActiveXObject("Msxml2.XMLHTTP");

}
else if(window.ActiveXObject){
xmlobj = new ActiveXObject("Micorsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlobj = new XMLHttpRequest();
}
}
*/

function Validate(){

CreateXMLHttpRequest();
var showurl = "yanzheng2.php?username=" + encodeURI(document.getElementById("username").value);

alert(showurl);

xmlobj.open("GET",showurl,true);
xmlobj.onreadystatechange = StatHandler;
xmlobj.send(null);

}

function StatHandler(){

if(xmlobj.readyState == 4 && xmlobj.status == 200){

alert(xmlobj.responseText);
if(xmlobj.responseText == "1"){
document.getElementById("msg").innerHTML="<font color=red>該用戶名已被人使用</font>";
}
else if(xmlobj.responseText == "0"){
document.getElementById("msg").innerHTML="<font color=green>該用戶名未被人使用</font>";
}
else{
document.getElementById("msg").innerHTML="<font color=red>用戶名驗證程序出錯</font>";
}
}

}
</script>

</head>

<body>
<p><form action="">
用戶名:<input type="text" id="username">
<input type="button" value="用戶名驗證" onclick="Validate();">
<div id="msg"></div>
</form></p>
</body>
</html>

 

<?
//yanzheng2.php

header('Content-Type:text/html; charset=utf8');
$conn = mysql_connect("localhost","root","");
mysql_select_db("user",$conn);

$user = urldecode($_GET["username"]);

mysql_query('set names utf8');
$querySQL = "select * from users where name= '$user'";

//echo $querySQL;


$rs = mysql_query($querySQL) or die("Error");
$rs_cnt = mysql_num_rows($rs);
echo $rs_cnt;
?>

 

其中還得建一張表 users

轉碼問題1:js代碼; [php] url = "register.php?username="+encodeURIComponent(username); [/php] php代碼: [php] //如果頁面編碼是gbk $username = mb_convert_encoding($username,'gb2312','utf-8'); //如果頁面編碼是utf-8 就不用上面了。 [/php]
轉碼問題2:例如:

修改 [php] var url="register.php?userName="+encodeURI(username); var url="action.php?userName="+encodeURI(username)+"&sex="+sex+"&userPwd="+pwd+"&realName="+encodeURI(realname)+"&email="+email+"&qq="+qq+"&tel="+tel+"&handSet="+handset+"&pos="+pos+"&address="+address; [/php] 在action.php中修改 [php] $username=urldecode($username); $realname=urldecode($realname); [/php]

 

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved