您的位置:首页 > 编程语言 > PHP开发

AJAX结合PHP代码实现登录

2009-01-02 10:52 567 查看
<script type="text/javascript"><!--
google_ad_client = "pub-4490194096475053";
/* 内容页,300x250,第一屏 */
google_ad_slot = "3685991503";
google_ad_width = 300;
google_ad_height = 250;
// --></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

<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){ // ForInternetExplorer 

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> 

javascript:;" onClick="javascript:tagshow(event, 'PHP');" target="_self">PHP脚本部分login.php: 

<? 

$username=$_GET["username"]; 

$password=$_GET["password"]; 

if($username=="admin" && $password=="admin"){ 

echo "yes"; 

}else{ 

echo "No"; 



?> 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息