Simple Example: AJAX combined with PHP code to achieve logon result code _ php instance

Source: Internet
Author: User
It is relatively simple to implement the login function through ajax + php. This is a simple example. Fixed characters can be read from the database in practical applications. HTML section:



Function postRequest (strURL ){
Var xmlHttp;
If (window. XMLHttpRequest) {// For Mozilla, Safari ,...
Var xmlHttp = new XMLHttpRequest ();
}
Else if (window. ActiveXObject) {// For Internet Explorer
Var xmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
}
XmlHttp. open ('post', strURL, true );
XmlHttp. setRequestHeader ('content-type', 'application/x-www-form-urlencoded ');
XmlHttp. onreadystatechange = function (){
If (xmlHttp. readyState = 4 ){
Updatepage (xmlHttp. responseText );
}
}
XmlHttp. send (strURL );
}

Function updatepage (str ){
If (str = "yes "){
Alert ("Welcome User ");
} Else {
Alert ("Invalid Login! Please try again! ");
}
}

Function call_login (){
Var username = parameter Doc ument. f1.username. value;
Var password = parameter Doc ument. f1.password. value;
Var url = "login. php? Username = "+ username +" & password = "+ password;
PostRequest (url );
}













PHP script login. php:

$ Username = $ _ GET ["username"];
$ Password = $ _ GET ["password"];
If ($ username = "admin" & $ password = "admin "){
Echo "yes ";
} Else {
Echo "No ";
}
?>
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.