使用php+ajax實現登入功能教程

來源:互聯網
上載者:User

php登入源碼:
下面是chklogin.php頁面代碼:
<?php
session_start();
$connect=mysql_connect("127.0.0.1","root","123");
if(!$connect)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db(design,$connect);
$query="select * from ta_user where user_name ='".$name."'";
$result=mysql_query($query) OR die("Unable to connect to MySQL");
$rows=mysql_fetch_array($result);
if($rows[user_password]==$pwd)
{

header("Location:login_suss.php?name=".$name."");
}
else
{
echo "<table width='235' height='61' border='0' align='center'>";
echo "<tr><td height='29'>使用者名稱或密碼錯誤!</td></tr>";
echo "<tr><td height='29'>請點擊<a href='javascript:history.go(-1);'>這兒</a>重新輸入!";
echo "</td></tr>";
echo "</table>";

}
?>

登陸驗證的AJAX程式碼範例:

<script language="javascript">
function getXMLHTTPRequest()
{
var xRequest=null;
if (window.XMLHttpRequest)
{
xRequest=new XMLHttpRequest();
}
else if (typeof ActiveXObject != "undefined")
{
xRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
return xRequest;
}
function Ajax(url)
{
AjaxObj = getXMLHTTPRequest();
AjaxObj.onreadystatechange = processRequest;
AjaxObj.open("post",url,true);
AjaxObj.setRequestHeader('Content-type','application/x-www-form-urlencoded');
AjaxObj.send("name"+name);

}
function processRequest()
{

if(AjaxObj.readyState == 4)
{
if(AjaxObj.status == 200)
{
if(AjaxObj.responseText != "")
{
document.getElementById("show").innerHTML="";
//alert(AjaxObj.responseText);

document.getElementById("show").innerHTML=AjaxObj.responseText;

}
}
else
{
alert("您所請求的頁面有異常。")
}
}
else
{
document.getElementById("show").innerHTML="Loading......";
}
}
</script> 
實際應用中,使用;調用就行..



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.