Ajax combined with PHP code to implement login

Source: Internet
Author: User
Tags php code php script

HTML section:

<scrīpt language= "Javascrīpt" >
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 = Window.document.f1.username.value;
var password = window.document.f1.password.value;
var url = "Login.php?username=" + username + "&password=" +password;
Postrequest (URL);
}
</scrīpt>
<body>
<Center>
<form name= "F1" ōnsubmit= "return Call_login ();" >
<table border= "0" bgcolor= "#CCCCCC" cellspacing= "1" cellpadding= "3" width= "316" >
<tr>
&LT;TD align= "left" colspan= "2" ><b><font size= "5" color= "#000080" >login</font></b></ Td>
</tr>
<tr>
&LT;TD align= "Right" width= "124" ><b><font color= "#000080" >user
Name:</font></b></td>
&LT;TD width= "177" ><input type= "text" name= "username" id= "user" size= "value=" "/></td>"
</tr>
<tr>
&LT;TD align= "Right" width= "124" ><b><font color= "#000080" >password:</font></b></td >
&LT;TD width= "177" ><input type= "password" name= "password" size= "value=" "/></td>"
</tr>
<tr>
&LT;TD colspan= "2" align= "center" ><input type= "button" name= "A1" value= "Login" ōnclick= "Call_login ()" ></ Td>
</tr>
</table>
</form>
</center>
</body>

PHP Script Part login.php:

<?
$username=$_GET["username"];
$password=$_GET["password"];
if($username=="admin" && $password=="admin"){
echo "yes";
}else{
echo "No";
}
?>

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.