PHP + Ajax checks whether the user name or email has been registered with an instance tutorial. ajax instance tutorial

Source: Internet
Author: User

PHP + Ajax checks whether the user name or email has been registered with an instance tutorial. ajax instance tutorial

PHP + Ajax checks whether a user name or email already exists during registration is a common and important function in the forum or membership system. This article describes how to implement this function in the form of an instance. The procedure is as follows:

I. PHP detection page

The check. php Page code is as follows:

<Script type = "text/javascript" src = "jiance. js "> </script> <form name =" myform "action =" "method =" get "> User name: <input name = "user" value = "" type = "text" onblur = "funtest100 () "/> <div id =" test100 "> </div> </form>

Ii. Ajax verification page

The check. js Page code is as follows:

Var xmlHttp; function S_xmlhttprequest () {if (window. activeXobject) {xmlHttp = new ActiveXObject ('Microsoft. XMLHTTP ');} else if (window. XMLHttpRequest) {xmlHttp = new XMLHttpRequest () ;}} function funtest100 () {var f = document. getElementsByTagName_r ('form') [0]. user. value; // get the content of the text box S_xmlhttprequest (); xmlHttp. open ("GET", "jcfor. php? Id = "+ f, true); // open the xmlHttp request. onreadystatechange = byphp; // you are ready to execute xmlHttp. send (null); // send} function byphp () {// judge the status if (xmlHttp. readyState = 1) {// Ajax status document. getElementByIdx_x_x ('test100 '). innerHTML = "loading";} if (xmlHttp. readyState = 4) {// Ajax status if (xmlHttp. status = 200) {// server status var bytest100 = xmlHttp. responseText; // alert (bytest100); document. getElementByIdx_x_x ('test100 '). innerHTML = bytest100 ;}}}

Iii. PHP verification page

The chkfor. php Page code is as follows:

<? Php if ($ _ GET [id]) {sleep (1); $ conn = mysql_connect ('localhost', 'root', ''); mysql_select_db ('test ', $ conn); $ SQL = "SELECT * FROM 'user' WHERE 'name' = '$ _ GET [id]'"; $ q = mysql_query ($ SQL ); if (is_array (mysql_fetch_row ($ q) {echo "user name already exists";} else {echo "user name can be used" ;}}?>

I hope this example will be helpful for PHP program development.


The friend has the PHP + AJAX code to check the user name.

Files include:

Userreg.html (registration page)
Ajaxreg. js (AJAX script and JS script for real-time verification)
Checkuserreg. php (connect to the database and check whether the user name has been registered)
Userreg.html (registration page) copy the PHP content to the clipboard
PHP code:
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>
<Script language = "javascript" src = "ajaxreg. js"> </script>
<Script language = "JavaScript" type = "text/JavaScript">
Function check () {// when the user name is null
If (document. reg. username. value = ""){
Document. getElementById ('check'). innerHTML = "<font color = red> the user name cannot be blank! </Font> ";
Document. reg. username. focus ();
Return false;
}
If (document. getElementById ('check '). innerHTML = "<font color = red> The number is registed </font> ") {// when The user name has been registered (<font color = red> The number is registed </font> is returned by AJAX)
Document. reg. username. focus ();
Return false;
}
If (document. reg. userpwd. value = "") {// when the password is empty
Document. getElementById ('pwd'). innerHTML = "<font color = red> the user password cannot be blank! </Font> ";
Document. reg. userpwd. focus ();
Return false;
}
If (document. reg. userpwd. value. length <6) {// when the password length is incorrect
Document. getElementById ('pwd'). innerHTML = "<font color = red> the password length cannot be less than 6 characters! </Font> ";
Document. reg. userpwd. focus ();
Return false;
}

If (document. reg. reuser ...... remaining full text>
 
Use javascript ajax php to check whether the registered user name already exists

<Script type = 'text/javascript '>
Var username = $ ('# username'). val ();
$. Post ('xx. php', {username: username}, function (data ){
If (data = 1 ){
Alert ("already exists"); // you can set a span or div to display '{xx'{.html ("already exists ");
}
});
</Script>
Xx. php
$ Username = $ _ POST ['username'];
$ SQL = "select * from user where username = $ username ";
$ Handle = mysql_query ($ SQL );
$ Num = mysql_num_rows ($ handle );
If ($ num> 0 ){
$ Flag = 1;
} Else {
$ Flag = 0;
}
Exit ($ flag );

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.