Then create a new file form.html
--------------------------form.html----------------------------
| The code is as follows |
Copy Code |
< Script src= "Ajax.js" ></script> <!--reference ajax classes <script language= "JavaScript type=" text/ JavaScript Function Show (username) { var ajax = Ajax (); var noteobj = document.getElementById ("note"); Object The label with the ID note to ajax.post ("form.php", {username:username},function (data) {//second username to the value that needs to be passed N oteobj.innerhtml = data; Data is }) obtained from the server side; } </script> <style> Input{font:menu}; </style> <form action= "reg.php" method= "POST" username:<input type= "text" name= "username" Onblur= "Show (This.value)"/><div id= "note" ></div><br/> password:<input type= "Password" Name= "password" ><br/> <input type= "Submit" value= "register" </form> |
Finally create a new PHP file form.php
--------------------------form.html----------------------------
| code is as follows |
copy code |
<?php Header ("content-type:text/html;charset=gb2312");//Set character set $m Ysqli = new Mysqli ("localhost", "root", "123", "Demo"); Open the demo database $result = $mysqli->query ("select * from Zhanghao where name= ' {$_post[" username "]}"); if ($result->num_rows > 0) {//Judge whether query out data echo "<font color= ' Red ' > user {$_post[" username "]} already exists! </font> "; }else{ Echo "<font color= ' green ' > user {$_post[' username ']} can register </font>; |
opens in the browser form.html the following image appears:
Note: You need to create a table "Zhanghao" in the MySQL database ahead of time.
If you enter a name that already exists, you receive the following prompt:
If you enter a name that does not exist, you receive the following prompt: