index.php
1 <html> 2 <head> 3 <title>在PHP中应用AJAX技术检测用户名</title> 4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 5 <style type="text/css"> 6 <!-- 7 body { 8 margin-bottom: 0px; 9 background-color: #64284A; 10 } 11 td{ 12 font-size:12px; 13 } 14 # style5 { 15 font-family: "文鼎淹水体"; 16 font-size: 18pt; 17 color: #64284A; 18 } 19 # style2 {font-size: 16px; font-family: "文鼎霹雳体"; color: #64284A;} 20 # style3 {font-family: "文鼎圆立体"} 21 # style4 {font-family: "文鼎淹水体"} 22 --> 23 </style> 24 </head> 25 <body> 26 <script language="javascript"> 27 var http_request = false; 28 function createRequest(url) { 29 //初始化对象并发出XMLHttpRequest请求 30 http_request = false; 31 if (window.XMLHttpRequest) 32 { //Mozilla等其他浏览器 33 http_request = new XMLHttpRequest(); 34 if (http_request.overrideMimeType) 35 { 36 http_request.overrideMimeType("text/xml"); 37 } 38 } 39 else if (window.ActiveXObject) 40 { //IE浏览器 41 try 42 { 43 http_request = new ActiveXObject("Msxml2.XMLHTTP"); 44 } 45 catch (e) 46 { 47 try 48 { 49 http_request = new ActiveXObject("Microsoft.XMLHTTP"); 50 } 51 catch (e) {} 52 } 53 } 54 if (!http_request) { 55 alert("不能创建XMLHTTP实例!"); 56 return false; 57 } 58 http_request.onreadystatechange = alertContents; //指定响应方法 59 60 http_request.open("GET", url, true); //发出HTTP请求 61 http_request.send(null); 62 } 63 function alertContents() { //处理服务器返回的信息 64 if (http_request.readyState == 4) { 65 if (http_request.status == 200) 66 { 67 alert(http_request.responseText); 68 } 69 else 70 { 71 alert('您请求的页面发现错误'); 72 } 73 } 74 } 75 </script> 76 <script language="javascript"> 77 function checkName() { 78 var username = form1.username.value; 79 if(username=="") { 80 window.alert("请填写用户名!"); 81 form1.username.focus(); 82 return false; 83 } 84 else 85 { 86 createRequest('checkname.php?username='+username+'&nocache='+new Date().getTime()); 87 } 88 } 89 </script> 90 <form name="form1" method="post" action=""> 91 <table width="1003" height="628" border="0" align="center" cellpadding="0" cellspacing="0"> 92 <tr> 93 <td valign="top" background="images/bg.JPG"><table width="1003" border="0" cellspacing="0" cellpadding="0"> 94 <tr> 95 <td width="242" height="346"> </td> 96 <td width="532" height="30" align="center" valign="bottom" class="style5"><span class="style2">用户注册<span class="style3"> <span class="style4"></span></span></span></td> 97 <td width="229"> </td> 98 </tr> 99 <tr> 100 <td> </td> 101 <td align="center"><table width="92%" border="0" cellpadding="0" cellspacing="0"> 102 <tr> 103 <td width="29%" height="30" align="center">用 户 名:</td> 104 <td width="71%" height="24"><input name="username" type="text" id="username" size="20"> 105 <a href="#" onClick="checkName();">[检测用户名]</a> </td> 106 </tr> 107 <tr> 108 <td height="24" align="center">真实姓名:</td> 109 <td height="24"><input name="truename" type="text" id="truename" size="20"> 110 *</td> 111 </tr> 112 <tr> 113 <td height="24" align="center">密 码:</td> 114 <td height="24"><input name="pwd1" type="password" id="pwd1" size="20"> 115 *</td> 116 </tr> 117 <tr> 118 <td height="24" align="center">确认密码:</td> 119 <td height="24"><input name="pwd2" type="password" id="pwd2" size="20"> 120 * </td> 121 </tr> 122 <tr> 123 <td height="24" align="center">性 别:</td> 124 <td height="24"><input name="sex" type="radio" value="男" checked> 125 男 126 <input type="radio" name="sex" value="女"> 127 女</td> 128 </tr> 129 <tr> 130 <td height="24" align="center" style="padding-left:10px">Email:</td> 131 <td height="24" class="word_grey"><input name="email" type="text" id="email" size="28"> 132 *</td> 133 </tr> 134 <tr> 135 <td height="24" align="center">个人主页:</td> 136 <td height="24" class="word_grey"><input name="homepage" type="text" id="homepage" size="28"></td> 137 </tr> 138 <tr> 139 <td height="40" colspan="2"> 140 <input name="imageField" type="image" src="images/submit.gif" width="64" height="20" border="0"> 141 142 <input name="imageField2" type="image" src="images/reset.gif" width="64" height="20" border="0"></td> 143 </tr> 144 </table></td> 145 <td> </td> 146 </tr> 147 </table></td> 148 </tr> 149 </table> 150 </form> 151 </body> 152 </html>
checkname.php
View Code
1 <?php 2 header('Content-type: text/html;charset=GB2312'); //指定发送数据的编码格式为GB2312 3 $link=mysql_connect("localhost","root","88888888"); 4 mysql_select_db("test",$link); 5 $GB2312string=iconv( 'UTF-8', 'gb2312//IGNORE' , $RequestAjaxString); //Ajax中先用encodeURIComponent对要提交的中文进行编码 6 mysql_query("set names gb2312"); 7 $username=$_GET[username]; 8 $sql=mysql_query("select * from tb_user where username='".$username."'"); 9 $info=mysql_fetch_array($sql); 10 if ($info){ 11 echo "很报歉!用户名[".$username."]已经被注册!"; 12 }else{ 13 echo "祝贺您!用户名[".$username."]没有被注册!"; 14 } 15 ?>