1. Build an AJAX development framework with the following code
<script language= "javascript" >var http_request = false;function createrequest (URL) {//Initialize the object and issue a XMLHttpRequest request Http_request = false; if (window. XMLHttpRequest) {//mozilla and other browsers http_request = new XMLHttpRequest (); if (http_request.overridemimetype) {http_request.overridemimetype ("text/xml"); }} else if (window. ActiveXObject) {//ie browser try {http_request = new ActiveXObject ("Msxml2.xml HTTP "); } catch (e) {try {http_request = new ActiveXObject ("Microsoft.XMLHTTP"); } catch (E) {}}} if (!http_request) {alert ("Cannot create XMLHTTP instance!"); return false; } http_request.onreadystatechange = alertcontents; Specify the response method Http_request.ope N ("GET", url, True); Make an HTTP request http_request.send (NULL);} function alertcontents () {//processing the information returned by the server if (http_request.readystate = = 4) {if (Http_request.status = =) {alert (http_request.responsetext); } else {alert (' the page you requested found errors '); }}}</script>
2. Write the JavaScript Custom function CheckName (); To detect whether the user name is empty, when the user name is not empty, call the Createrequest () method to send the request to detect whether the user name exists, the code is as follows:
function CheckName () { var username = form1.user.value; if (username== "") { window.alert ("Enter user name"); } else { createrequest (' action/checkname.php?username= ' +username+ ' &nocache= ' +new Date (). GetTime ());} }
3. Add "Detect user name hyperlinks" on the page
<TD width= "class=" Ziti2 "><a href=" # "onclick=" CheckName () ">[Detect user name]</a></td>
4. Create a checkname.php processing page with the following code
<?phpheader ("Content-type= text/html; Charset=utf8 "); include". /conn/conn.php "; $GB 2312string=iconv (' UTF-8 ', ' Gb2312//ignore ', $RequestAjaxString); In Ajax, first use encodeURIComponent to encode the Chinese to be submitted $username = $_get[username]; $sql =mysql_query ("select * from user where username = ' ". $username." ") Or Die (Mysql_error ()); $info =mysql_fetch_array ($sql); if ($info) { echo "Congratulations!" user name [". $username."] Not registered! ";} else{ echo "Congratulations!" user name [". $username."] Not registered! ";}? >
Beginner PHP, in the study of the practice of recording problems, skills, code problems or have other comments welcome to make progress together ~
This article is from "Dream" blog, please be sure to keep this source http://snbo520.blog.51cto.com/3069187/1410510