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

2018-09-07 17:31

阅读:299

  HTML部分:
<html>
<head>
<scrīptlanguage=javascrīpt>
functionpostRequest(strURL){
varxmlHttp;
if(window.XMLHttpRequest){//ForMozilla,Safari,...
varxmlHttp=newXMLHttpRequest();
}
elseif(window.ActiveXObject){//ForInternetExplorer
varxmlHttp=newActiveXObject(Microsoft.XMLHTTP);
}
xmlHttp.open(POST,strURL,true);
xmlHttp.setRequestHeader(Content-Type,application/x-
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
updatepage(xmlHttp.responseText);
}
}
xmlHttp.send(strURL);
}

functionupdatepage(str){
if(str==yes){
alert(WelcomeUser);
}else{
alert(InvalidLogin!Pleasetryagain!);
}
}

functioncall_login(){
varpassword=window.document.f1.password.value;
varurl=login.php?username=+username+&password=+password;
postRequest(url);
}
</scrīpt>
</head>

<body>
<Center>

<formname=f1ōnSubmit=returncall_login();>
<tableborder=0bgcolor=#CCCCCCcellspacing=1cellpadding=3width=316>
<tr>
<tdalign=leftcolspan=2><b><fontsize=5color=#000080>Login</font></b></td>
</tr>
<tr>
<tdalign=rightwidth=124><b><fontcolor=#000080>User
Name:</font></b></td>
<tdwidth=177><inputtype=textname=usernameid=usersize=20value=/></td>
</tr>
<tr>
<tdalign=rightwidth=124><b><fontcolor=#000080>Password:</font></b></td>
<tdwidth=177><inputtype=passwordname=passwordsize=20value=/></td>
</tr>
<tr>
<tdcolspan=2align=center><inputtype=buttonname=a1value=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){
echoyes;
}else{
echoNo;
}
?>


评论


亲,登录后才可以留言!