AJAX實現登入介面

來源:互聯網
上載者:User

標籤: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實現登入介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.