1,用户注册页 Login.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <html> <head> <title>用户注册_www.jb200.com</title> <script type= "text/<a href=" http: //www.jb200.com/js/" target="_blank" class="infotextkey">javascript</a>"> var xmlHttp; function createXMLHttpRequest() { //在IE下创建XMLHttpRequest对象 try { xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" ); } catch (e) { try { xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); } catch (oc) { xmlHttp = null ; } } //在Mozilla和Safari等非IE浏览器下创建XMLHTTPRequest对象 if (!xmlHttp && typeof XMLHttpRequest != "undefined" ) { xmlHttp = new XMLHttpRequest(); } return xmlHttp; } //方式请求 function startRequest() { //获取用户输入的信息 var UserID = document.getElementById( "UserID" ).value; //输入的用户名是否为空 if (UserID!= "" ) { //创建XMLHttpRequest对象 createXMLHttpRequest(); var url = "Login.php?UserID=" +encodeURI(UserID); //指定url xmlHttp.open( "GET" ,url, true ); xmlHttp.onreadystatechange = handleStateChange; //指定<a href="http://www.jb200.com/zt/huidiaohanshu/" target="_blank" class="infotextkey">回调函数</a> xmlHttp.send( null ); } else { alert( "请填写您的用户名!" ); } } function handleStateChange() { if (xmlHttp.readyState<4) { span.innerHTML= "正在读取数据..." ; } if (xmlHttp.readyState==4) { if (xmlHttp.status == 200) { span.innerHTML=xmlHttp.responseText; } else { alert( "错误,请求页面异常!" ); } } } </script> </head> <body> <form action= "#" > <center> <h3>Ajax+php验证用户名是否可用</h3> <table> <tr> <td>用户名: <input type= "text" name= "UserID" id= "UserID" onchange= "startRequest()" /> <span id= "span" ></span></td> </tr> <tr> <td>密 码: <input type= "text" name= "Pwd" id= "Pwd" /></td> </tr> <tr> <td> <br/> <input type= "button" name= "Reg" id= "Reg" value= "注册" /> <input type= "button" name= "Log" id= "Log" value= "登录" /> </td> </tr> </table> </center> <form> </body> </html> |
2,注册检测页 Login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php //设置PHP的显示语言 header( 'Content-Type:text/html;charset=UTF-8' ); //让程序先等待一秒 sleep(1); //获取输入的用户名 $tmp=$_GET[ "UserID" ]; $db_host= "localhost" ; $db_name= "canphp" ; $db_username= "root" ; $db_password= "rootpass" ; $link = <a href= "http://www.jb200.com/mysql/" target= "_blank" class= "infotextkey" >mysql</a>_connect($db_host, $db_username,$db_password ) or die ( 'Can' t connet db! ' . mysql_error()); mysql_select_db($db_name, $link) or die (' Can 't select db : ' . mysql_error()); $query= "select * from cp_user where username='$tmp'" ; $result = mysql_query($query) or die ($query.mysql_error()); $rows=mysql_num_rows($result); if ($rows>0) { //存在记录 echo "用户名已存在" ; } else { echo( "可以注册!" ); } ?> |