Chinaunix首页 | 论坛 | 博客
  • 博客访问: 580388
  • 博文数量: 107
  • 博客积分: 5065
  • 博客等级: 大校
  • 技术积分: 1362
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-29 16:01
文章存档

2009年(84)

2008年(23)

我的朋友

分类:

2009-01-14 20:57:41

简单示例:AJAX结合PHP代码实现登录
页面1: login.html

<html>
<head>
<script language="javascript">
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);
}
</script>

</head>


<body>
<Center>

<form name="f1" onSubmit="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" onClick="call_login()"></td>
    </tr>
</table>
</form>

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


页面2: login.php


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

阅读(1534) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~