AJAX ASP page Php+ajax implementation without flush registration (real-time detection with username)

Source: Internet
Author: User
Many times, we register personal information on the Internet, after submitting the page, we must wait for the page to refresh to tell us whether the registration is successful, when the network is poor, if the registration of a lot of things, after a long wait for the page refresh, get really "your user name has been used" or xxxxxxx illegal, I think everyone's mood must be particularly uncomfortable, today introduced an AJAX implementation page does not refresh registration + real-time detection of user information simple registration procedures, I hope to be helpful. OK, first look at the Registration interface code:






















· User name:


*
4-16 characters, English lowercase, Chinese characters, numbers, preferably not all numbers.
· User password:


*
The password letter is of a case. 6-16 digits (including 6, 16), limited in English, digital.
· Repeat Password:

*
Please enter your login password again.



The red part is the JS function to be called, when we select a text box, we will start to call, now we look at the above page contains the Ajaxreg.js file code, which contains the AJAX framework and some judgment function.
var Http_request=false;
function send_request (URL) {//Initialize, specify handler function, send request functions
Http_request=false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest) {//mozilla browser
Http_request=new XMLHttpRequest ();
if (http_request.overridemimetype) {//Set MIME category
Http_request.overridemimetype ("Text/xml");
}
}
else if (window. ActiveXObject) {//ie browser
try{
Http_request=new ActiveXObject ("msxml2.xmlhttp");
}catch (e) {
try{
Http_request=new ActiveXObject ("Microsoft.XMLHTTP");
}catch (e) {}
}
}
if (!http_request) {///exception, creation of object instance failed
Window.alert ("Failed to create XMLHTTP Object! ");
return false;
}
Http_request.>//Determine how to send the request, URL, and whether to synchronize the execution of the next segment of code
Http_request.open ("GET", url,true);
Http_request.send (NULL);
}
function to process return information
function ProcessRequest () {
if (http_request.readystate==4) {//Judging object state
if (http_request.status==200) {//information has been successfully returned to start processing information
document.getElementById (reobj). Innerhtml=http_request.responsetext;
}
else{//page is not working
Alert ("The page you requested is not working!") ");
}
}
}
function Usercheck (obj) {
var F=document.reg;
var Username=f.username.value;
if (username== "") {
document.getElementById (obj). innerhtml= "User name cannot be empty! ";
F.username.focus ();
return false;
}
else{
document.getElementById (obj). innerhtml= "reading data ...";
Send_request (' checkuserreg.php?username= ' +username);
Reobj=obj;
}
}
function Pwdcheck (obj) {
var F=document.reg;
var Pwd=f.userpwd.value;
if (pwd== "") {
document.getElementById (obj). innerhtml= "User password cannot be empty!" ";
F.userpwd.focus ();
return false;
}
else if (f.userpwd.value.length<6) {
document.getElementById (obj). innerhtml= "Password length cannot be less than 6 bits! ";
F.userpwd.focus ();
return false;
}
else{
document.getElementById (obj). innerhtml= "Password meets the requirements! ";
}
}
function Pwdrecheck (obj) {
var F=document.reg;
var Repwd=f.reuserpwd.value;
if (repwd== "") {
document.getElementById (obj). innerhtml= "Please enter the password again!" ";
F.reuserpwd.focus ();
return false;
}
else if (f.userpwd.value!=f.reuserpwd.value) {
document.getElementById (obj). innerhtml= "two times input passwords are inconsistent! ";
F.reuserpwd.focus ();
return false;
}
else{
document.getElementById (obj). innerhtml= "Password entered correctly! ";
}
}
It is not difficult to see, the data is transmitted through the asynchronous transmission to the checkuserreg.php and then return the information displayed to achieve real-time detection of user name purposes, as for the password, only for the length of the real-time judgment, interested friends can expand the function. Let's see what checkuserreg.php has done:
Header (' content-type:text/html;charset=gb2312 ');//Avoid garbled output
Include (' inc/config.inc.php ');//contains database basic configuration information
Include (' inc/dbclass.php ');//contains database operation class
$username =trim ($_get[' username ');//Get registered Name
//-----------------------------------------------------------------------------------
$db =new db;//to generate an instance from a database operation class
$db->mysql ($dbhost, $dbuser, $dbpassword, $dbname);//Call the connection parameter function
$db->createcon ();//call to create a connection function
//-----------------------------------------------------------------------------------
$querysql = "Select username from cr_userinfo where username= ' $username '";//Query member name
$result = $db->query ($querysql);
$rows = $db->loop_query ($result);
If the member name is registered
//-----------------------------------------------------------------------------------
if ($rows) {
echo "This member name has already been registered, please change the member name!" ";
}
If the member name is not registered, the display
//-----------------------------------------------------------------------------------
else{
echo "This member name can be registered!" ";
}
if ($action ==reg) {
$addsql = "INSERT INTO Cr_userinfo
VALUES (0, ' $username ', ' $userpwd ', ' $time ', 50,1, ' $userquestion ', ' $useranswer ');
$db->query ($addsql);
echo "Congratulations, the registration is successful!" Please click here to login! ";
}
$db->close ();//Close database connection
?>
Comments written in detail, we should all be able to understand, and then look at the information legally after we submit registration information to implement a non-flush registration of PHP code, senduserinfo.php:
Header (' content-type:text/html;charset=gb2312 ');//Avoid garbled output
Include (' inc/config.inc.php ');//contains database basic configuration information
Include (' inc/dbclass.php ');//contains database operation class
$username =trim ($_get[' username ');//Get registered Name
$userpwd =md5 (Trim ($_get[' userpwd '));//Get Registration Password
$time =date ("y-m-d");
//-----------------------------------------------------------------------------------
$db =new db;//to generate an instance from a database operation class
$db->mysql ($dbhost, $dbuser, $dbpassword, $dbname);//Call the connection parameter function
$db->createcon ();//call to create a connection function
//-----------------------------------------------------------------------------------
Start inserting data
//-----------------------------------------------------------------------------------
$addsql = "INSERT into Cr_userinfo values (0, ' $username ', ' $userpwd ', ' $time ', 50,1, ' $userquestion ', ' $useranswer ')";
$db->query ($addsql);
echo "Congratulations, the registration is successful!" Please click here to login! ";
$db->close ();//Close database connection
?>
Ok!! Done, take a look:
1.

2.

3.

4.

5.

What do you think? Not bad, put so much tired, I hope you like ~ ~ ~ ~

The above describes the Ajax ASP Php+ajax implementation of non-flush registration (with the user name real-time detection), including the Ajax ASP content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.