簡單示例AJAX結(jié)合PHP代碼實現(xiàn)登錄效果代碼
更新時間:2008年07月25日 08:58:51 作者:
比較簡單的通過ajax+php實現(xiàn)登陸功能,這是個簡單的例子,固定字符,實際應用中可以從數(shù)據(jù)庫中讀取
HTML部分:
<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){ // 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);
}
</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>
PHP腳本部分login.php:
<?
$username=$_GET["username"];
$password=$_GET["password"];
if($username=="admin" && $password=="admin"){
echo "yes";
}else{
echo "No";
}
?>
<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){ // 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);
}
</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>
PHP腳本部分login.php:
<?
$username=$_GET["username"];
$password=$_GET["password"];
if($username=="admin" && $password=="admin"){
echo "yes";
}else{
echo "No";
}
?>
相關文章
php+memcache實現(xiàn)的網(wǎng)站在線人數(shù)統(tǒng)計代碼
這篇文章主要介紹了php+memcache實現(xiàn)的網(wǎng)站在線人數(shù)統(tǒng)計代碼,代碼例子簡潔實用,需要的朋友可以參考下2014-07-07php 二維數(shù)組快速排序算法的實現(xiàn)代碼
這篇文章主要介紹了php 二維數(shù)組快速排序算法的實現(xiàn)代碼的相關資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10PHP獲取IP地址所在地信息的實例(使用純真IP數(shù)據(jù)庫qqwry.dat)
下面小編就為大家?guī)硪黄狿HP獲取IP地址所在地信息的實例(使用純真IP數(shù)據(jù)庫qqwry.dat)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11