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]