標籤:turn 簡單 include text tle body roo button location
使用php跳轉介面和AJAX都可實現登入介面的跳轉的登入失敗對的提醒。但是,php跳轉的方式
需要額外載入其他介面,使用者體驗差。AJAX可實現當前頁面只重新整理需要的資料,不對當前網頁進行
重新載入或者是跳轉。
做一個簡單的登入介面:
<div id="">使用者名稱 :<input type="text" name="" id="uid" value="" /></div><div id="">密 碼 :<input type="text" name="" id="pwd" value="" /></div><div id=""><input type="button" name="" id="denglu" value="登入" /></div>
然後是js代碼,使用jq比較簡單,所以要先引入jq檔案包。
其次擷取使用者名稱和密碼的值:
var uid = $("#uid").val();var pwd = $("#pwd").val();
設定好之後就可以進行AJAX的設定了:
$.ajax({type: "post",url: "dengluchuli.php",data: {u: uid,p: pwd},dataType: "TEXT",success: function(r) { //r為傳回值if(r.trim() == "y") { //y為 url跳轉網頁中傳回的值。window.location.href = "跳轉介面";} else {alert("使用者名稱或密碼錯誤");}}});
dengluchuli.php:
<?phpinclude("AJAXLOGIN.class.php");$dd = new LoGin($_POST["u"],$_POST["p"]); $ae = $dd::logi("login","username","password","name"); //分別是資料庫中的 表名,表中的使用者名稱,密碼,姓名(資料從資料庫中匯入)?>
這裡我引入了一個登入類(AJAXLOGIN.class.php),是為了以後方便使用:
<?phpclass DBDA{public $host="localhost";public $uid = "root";public $pwd = "";public $dbname = "12345";//成員方法public function Query($sql,$type=1){$db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);$r = $db->query($sql);if($type==1){return $r->fetch_all();}else{return $r;}}}class LoGin{ public static $uid ;public static $pwd ;function __construct($x,$y) { self::$uid = $x; self::$pwd = $y; } static function logi ($table,$username,$password,$name){$db = new DBDA();$nnn = self::$uid;$sql = " select $name,$password from $table where $username=‘$nnn ‘" ;$at = $db->Query($sql);$p = self::$pwd;if(!empty($p) && $p==$at[0][1]){$_SESSION["uid"]= $at[0][0];echo "y";}else{echo "n";}}}?>
登入介面完整代碼:
<!doctype html><html lang="en"><head><meta charset="UTF-8" /><title>Document</title><script src="../jquery-1.11.2.min.js" type="text/javascript" charset="utf-8"></script></head><body><div id="">使用者名稱 :<input type="text" name="" id="uid" value="" /></div><div id="">密 碼 :<input type="text" name="" id="pwd" value="" /></div><div id=""><input type="button" name="" id="denglu" value="登入" /></div></body><script type="text/javascript">$("#denglu").click(function() {var uid = $("#uid").val();var pwd = $("#pwd").val();$.ajax({type: "post",url: "dengluchuli.php",data: {u: uid,p: pwd},dataType: "TEXT",success: function(r) {if(r.trim() == "y") {window.location.href = "ppp.php";} else {alert("使用者名稱或密碼錯誤");}}});})</script></html>
AJAX實現登入介面