简单示例:AJAX结合PHP代码实现登录
<P><STRONG><FONT size=2> 【PHPChina讯】 <U><STRONG>javascript</STRONG></U>:tagshow(event, 'HTML');" href="javascript:;" target=_self><U><STRONG>HTML</STRONG></U></A>部分:</FONT></STRONG><BR></P><P><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 <U><STRONG>Internet</STRONG></U> 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><BR>function updatepage(str){<BR>if(str=="yes"){<BR>alert("Welcome User");<BR>}else{<BR>alert("Invalid Login! Please try again!");<BR>}<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><BR><body><BR><Center><BR><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><BR></center><BR></body><BR></html><BR></P><P><FONT size=2><STRONG><U><STRONG>PHP</STRONG></U>脚本部分login.php:</STRONG></FONT></P><P><STRONG><FONT size=2></FONT></STRONG><?<BR>$username=$_GET["username"];<BR>$password=$_GET["password"];<BR>if($username=="admin" && $password=="admin"){<BR>echo "yes";<BR>}else{<BR>echo "No";<BR>}<BR>?></P><P>原文地址:</P>
页:
[1]