Ajax与PHP结合实现登录验证
2008-12-03 22:36
459 查看
Step 1:
Create a html file as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>A guide to ajax</title>
<script type="text/javascript" language="javascript">
var xmlHttp=null;
//create the XMLHttpRequest object
function createRequest() {
try {
xmlHttp = new XMLHttpRequest();
} catch (tryMS) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlHttp = null;
}
}
}
return xmlHttp;
}
//get the querystring
function createQueryString(){
var userId=document.getElementById("userId").value;
var password=document.getElementById("password").value;
var query="userId="+escape(userId)+"&password="+escape(password);
return query;
}
//action
function login(){
createRequest();
var queryString=createQueryString();
var url="login.php?timeStamp="+ new Date().getTime();
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 ){
if(xmlHttp.status==200)
document.getElementById("result").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.send(queryString);
}
</script>
</head>
<body>
<form action="#">UserId:<input type="text" name="userId"
id="userId" /> Password:<input type="password" name="password"
id="password" /> <br />
<input type="button" onclick="login();" value="login" /></form>
<div id="result"></div>
</body>
</html>
Step 2:
Create a php file:
<?php
//open a connection to database server
$conn = mysql_connect ( "localhost", "root", "admin" );
if (! $conn)
die ( "Connecting to datatabase error:" . mysql_error () );
//select a database
if (! mysql_select_db ( "zry_db", $conn )) {
die ( "Error select the first database:" . mysql_error () );
}
//get the post variablity
$userId = $_POST ["userId"];
$userPwd = $_POST ["password"];
//valid input
if (empty ( $userId )) {
echo "userId required!<br/>";
}
if (empty ( $userPwd )) {
echo "password required!<br/>";
}
//if userId and password not empty
if (! empty ( $userId ) && ! empty ( $userPwd )) {
$sql = "SELECT userId,password FROM login WHERE userId='" . $userId . "' AND password='" . $userPwd . "'";
$result = mysql_query ( $sql, $conn );
if (mysql_num_rows ( $result ) > 0) {
echo "Login Succeed";
} else {
echo "Login Failed";
}
}
//close the connection
@mysql_close ( $conn );
?>
Just so simple!
Create a html file as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>A guide to ajax</title>
<script type="text/javascript" language="javascript">
var xmlHttp=null;
//create the XMLHttpRequest object
function createRequest() {
try {
xmlHttp = new XMLHttpRequest();
} catch (tryMS) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlHttp = null;
}
}
}
return xmlHttp;
}
//get the querystring
function createQueryString(){
var userId=document.getElementById("userId").value;
var password=document.getElementById("password").value;
var query="userId="+escape(userId)+"&password="+escape(password);
return query;
}
//action
function login(){
createRequest();
var queryString=createQueryString();
var url="login.php?timeStamp="+ new Date().getTime();
xmlHttp.open("post",url,true);
xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4 ){
if(xmlHttp.status==200)
document.getElementById("result").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.send(queryString);
}
</script>
</head>
<body>
<form action="#">UserId:<input type="text" name="userId"
id="userId" /> Password:<input type="password" name="password"
id="password" /> <br />
<input type="button" onclick="login();" value="login" /></form>
<div id="result"></div>
</body>
</html>
Step 2:
Create a php file:
<?php
//open a connection to database server
$conn = mysql_connect ( "localhost", "root", "admin" );
if (! $conn)
die ( "Connecting to datatabase error:" . mysql_error () );
//select a database
if (! mysql_select_db ( "zry_db", $conn )) {
die ( "Error select the first database:" . mysql_error () );
}
//get the post variablity
$userId = $_POST ["userId"];
$userPwd = $_POST ["password"];
//valid input
if (empty ( $userId )) {
echo "userId required!<br/>";
}
if (empty ( $userPwd )) {
echo "password required!<br/>";
}
//if userId and password not empty
if (! empty ( $userId ) && ! empty ( $userPwd )) {
$sql = "SELECT userId,password FROM login WHERE userId='" . $userId . "' AND password='" . $userPwd . "'";
$result = mysql_query ( $sql, $conn );
if (mysql_num_rows ( $result ) > 0) {
echo "Login Succeed";
} else {
echo "Login Failed";
}
}
//close the connection
@mysql_close ( $conn );
?>
Just so simple!
相关文章推荐
- PHP结合Ajax实现登录验证的Demo
- 安卓服务端开发(1) 安卓结合PHP实现连接数据库验证登录功能(附全部代码)
- AJAX结合PHP代码实现登录
- PHP与Ajax相结合实现登录验证小Demo
- 简单示例AJAX结合PHP代码实现登录效果代码-2
- 简单示例AJAX结合PHP代码实现登录效果代码
- 简单示例AJAX结合PHP代码实现登录效果代码
- PHP与Ajax相结合实现登录验证小Demo
- Jquery中的AJax技术结合PHP实现无刷新验证验证码
- 简单示例:AJAX结合PHP代码实现登录
- php和ajax结合实现登录
- AJAX结合PHP代码实现登录
- 通过Ajax实现不显示登录框的IIS-Windows集成身份验证登录
- JQuery+Ajax实现登录验证
- PHP + ajax 实现异步登录验证
- php结合ajax实现手机发红包的案例
- JqueryAjax+php实现简单的注册登录
- PHP+jQuery+Ajax实现用户登录与退…
- jQuery的ajax实现登录验证
- jQuery+AJAX实现遮罩层登录验证界面(附源码)