«
AJAX结合PHP代码实现登录

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  HTML部分:</p><code><html><br /><head><br /><scrīpt language="javascrīpt"><br />function postRequest(strURL){<br />var xmlHttp;<br />if(window.XMLHttpRequest){ // For Mozilla, Safari, ...<br />var xmlHttp = new XMLHttpRequest();<br />}<br />else if(window.ActiveXObject){ // For Internet Explorer<br />var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");<br />}<br />xmlHttp.open('POST', strURL, true);<br />xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');<br />xmlHttp.onreadystatechange = function(){<br />if (xmlHttp.readyState == 4){<br />updatepage(xmlHttp.responseText);<br />}<br />}<br />xmlHttp.send(strURL);<br />}<br />function updatepage(str){<br />if(str=="yes"){<br />alert("Welcome User");<br />}else{<br />alert("Invalid Login! Please try again!");<br />}<br />}<br />function call_login(){<br />var username = window.document.f1.username.value;<br />var password = window.document.f1.password.value;<br />var url = "login.php?username=" + username + "&password=" +password ;<br />postRequest(url);<br />}<br /></scrīpt><br /></head><br /><body><br /><Center><br /><form name="f1" ōnSubmit="return call_login();"><br /><table border="0" bgcolor="#CCCCCC" cellspacing="1" cellpadding="3" width="316"><br /><tr><br /><td align="left" colspan="2"><b><font size="5" color="#000080">Login</font></b></td><br /></tr><br /><tr><br /><td align="right" width="124"><b><font color="#000080">User<br />Name:</font></b></td><br /><td width="177"><input type="text" name="username" id="user" size="20" value="" /></td><br /></tr><br /><tr><br /><td align="right" width="124"><b><font color="#000080">Password:</font></b></td><br /><td width="177"><input type="password" name="password" size="20" value="" /></td><br /></tr><br /><tr><br /><td colspan="2" align="center"><input type="button" name="a1" value="Login" ōnClick="call_login()"></td><br /></tr><br /></table><br /></form><br /></center><br /></body><br /></html></code><p>  PHP脚本部分login.php:</p><code><?<br />$username=$_GET["username"];<br />$password=$_GET["password"];<br />if($username=="admin" && $password=="admin"){<br />echo "yes";<br />}else{<br />echo "No";<br />}<br />?></code>