An implementation example of user name detection based on AJAX implementation in PHP

Source: Internet
Author: User
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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.