站长资源网络编程
简单示例AJAX结合PHP代码实现登录效果代码
简介HTML部分: function postRequest(strURL){ var xmlHttp; if(window.XMLHttpRequest){ // For Mozilla, Safari, ...
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";
}
?>
最新资源
- 群星《天赐的声音第五季 第5期》[320K/MP3][118
- 群星《天赐的声音第五季 第5期》[FLAC/分轨][29
- 群星《歌手2024 第3期》[320K/MP3][78.45MB]
- 群星《歌手2024 第3期》[FLAC/分轨][204.16MB]
- 群星《乘风2024 第6期》[320K/MP3][57.95MB]
- 群星《乘风2024 第6期》[FLAC/分轨][170.72MB]
- 陈立强1995-我爱你胜过这世界[福茂][WAV+CUE]
- 群星-《2014年十大发烧唱片精选HQCD》[低速原抓
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限
- 林育群.2013-BalladShow(日本版)【环球】【WAV+