欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

简单示例AJAX结合PHP代码实现登录效果代码

程序员文章站 2022-07-01 15:25:44
html部分: function&n...
html部分:
<html>
<head>
<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>
</head>

<body>
<center>

<form name="f1" ōnsubmit="return call_login();">
<table border="0" bgcolor="#cccccc" cellspacing="1" cellpadding="3" width="316">
<tr>
<td align="left" colspan="2"><b><font size="5" color="#000080">login</font></b></td>
</tr>
<tr>
<td align="right" width="124"><b><font color="#000080">user
name:</font></b></td>
<td width="177"><input type="text" name="username" id="user" size="20" value="" /></td>
</tr>
<tr>
<td align="right" width="124"><b><font color="#000080">password:</font></b></td>
<td width="177"><input type="password" name="password" size="20" value="" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" name="a1" value="login" 
ōnclick="call_login()"></td>
</tr>
</table>
</form>

</center>
</body>
</html>


php脚本部分login.php:

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