Use php + Ajax to implement unique verification for code [simple application]

Source: Internet
Author: User

First create an Ajax class (Ajax class)

Then a new file form.html is created.

----------------------------Form.html ----------------------------
Copy codeThe Code is as follows:
<Script src = "ajax. js"> </script> <! -- Reference ajax class -->
<Script language = "javascript" type = "text/javascript">
Function show (username ){
Var ajax = Ajax ();
Var noteobj = document. getElementById ("note"); // visualize the tag with id as note
Ajax. post ("form. php", {username: username}, function (data) {// The second username is the value to be passed
Noteobj. innerHTML = data; // data is the data obtained from the server.
});
}
</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>

Create a new php file form. php
----------------------------Form.html ----------------------------
Copy codeThe Code is as follows:
<? Php
Header ("Content-type: text/html; charset = gb2312"); // sets the character set
$ Mysqli = 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) {// determines whether data is queried.
Echo "<font color = 'red'> the user {$ _ POST [" username "]} already exists! </Font> ";
} Else {
Echo "<font color = 'green'> Users {$ _ POST [" username "]} can register </font> ";
}
?>

In the browser, open form.html:

Note: You must create a "zhanghao" table in the MySQL database in advance"

If you enter an existing name, the following prompt is displayed:

If you enter a name that does not exist, the following prompt is displayed:

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.