jQuery+Pdo編寫login登陸界面
Jquery是繼prototype之后又一個優(yōu)秀的Javascript庫。而且它是輕量級的js庫。共有兩個版本的 jQuery 可供下載:一份是精簡過的,另一份是未壓縮的(供調(diào)試或閱讀)。這兩個版本都可從 jQuery.com 下載。所以開發(fā)學(xué)習(xí)建議使用壓縮的。
當(dāng)然啦,除了直接下載jquery.js文件外,還可以在html頭中插入Google或者Microsoft的CDN來在網(wǎng)頁加載時直接從網(wǎng)上獲取庫文件支持。
使用 Google 的 CDN:
<head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs /jquery/1.4.0/jquery.min.js"></script> </head>
使用 Microsoft 的 CDN:
<p style="margin-top: 12px; margin-bottom: 0px; color: rgb(51, 51, 51); line-height: 18px; font-family: Verdana, Arial, Helvetica, sans-serif; display: inline !important; background-color: rgb(249, 249, 249);"></p><pre name="code" class="html" style="color: rgb(51, 51, 51); font-weight: bold; line-height: 24px; display: inline !important;"><head>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery /jquery-1.4.min.js"></script>
</head>
但是最好是直接下載到自己的工程中來使用,不然有時會存在網(wǎng)絡(luò)問題而無法提供支持。
Pdo是用來代替Mysql或者mysql等來處理數(shù)據(jù)庫的。
只要在php.ini文件中去掉;extension=php_pdo......等被注釋掉的有關(guān)pdo的.dll文件。同時從php5開始,php默認(rèn)打開pdo驅(qū)動,所以你可能在php.ini文件中看不到php_pdo.dll(這是個人的理解或者說是猜測,網(wǎng)上教程說一定需要著個文件,但是我只是把其他與pdo相關(guān)的所有去注釋掉了而已,最后也能運行。)
去掉注釋后保存后就可以運行phpinfo來進行測試了之中可以看到有關(guān)pdo的相關(guān)信息。表示配置成功。
在上一篇文章中已經(jīng)用ajax技術(shù)編寫了一個login登陸界面,其實不用這么麻煩,可以試試用Jquery和PDO寫寫,感受感受。于是就粗略的學(xué)習(xí)了一下Jquery,這才發(fā)現(xiàn)只要一個$POST()函數(shù)就可以完成我之前所編寫的兩頁的代碼(當(dāng)然啦!人家的是已經(jīng)封裝做好的,只需直接調(diào)用就行了),但是我依然很高興直接寫了ajax,這使我對異步認(rèn)識的更加深刻。
好了直接上代碼。
首先是登陸界面的代碼(與之前的沒多大的區(qū)別,有一些小小改動,但是細(xì)節(jié)出魔鬼,可以認(rèn)真到什么程度,我就盡力吧!)
div.css
div.login { border: 1px solid #a0b1c4 ; height:429px; width:300px; position: absolute; left:1200px; top: 150px; }
login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8 " /> <script type="text/javascript" src="/login_Jquery/js/jquery-2.2.1.js"></script> //引用Jquery.js文件<script src="/login_Jquery/js/ClickME.js"></script> //引用自己編寫的.js文件<link rel="stylesheet" href="/login_Jquery/css/div.css" type="text/css" /> <title>登陸界面</title> </head> <body style=" text-align:center"> <h1>登陸界面</h1> <div class="login" > <form action="#" method="post" name="myform" id="myform"> <p> 用戶名:<input type="text" name="user" id="user" maxlength="20" /> </p> <p> 密 碼:<input type="password" name="pwd" id="pwd" maxlength="20" /> </p> <p> <div id="serverResponse"></div> </p> <img id="login" src="/loginProject/pictrue/login.png" /> </form> </div> </body> </html>
ClickME.js
$(document).ready(function(){ $("#login").click(function(){ var user= document.getElementById("user").value; var pwd= document.getElementById("pwd").value; if(user==""||pwd=="") { alert("用戶名與密碼不能為空!") } else { //重點在這,對比之前寫的xmlHttpRequest的代碼量可以看到,在這只是調(diào)用了一個 //$.post()函數(shù) $.post( "/login_Jquery/php/check.php", $("#myform").serialize(), function(msg){ $("#serverResponse").html(msg);} ); } }); });
<span style="font-size:32px;">$.post()函數(shù),參數(shù)格式是:</span>
<span style="font-size:32px;">$.post(url,data,call ,type)</span>
url是請求頁面的地址,data是用傳送的數(shù)據(jù),callback是響應(yīng)函數(shù),type是返回內(nèi)容的格式如text/xml等。就本代碼來說,url為"/login_Jquery/php/check.php" data:$("#myform").serialize(),其中serialize()函數(shù)是將要傳送的數(shù)據(jù)序列化便于傳送數(shù)據(jù)。
callback:function(msg){$("#serverResponse").html(msg); } msg表示服務(wù)器回傳的數(shù)據(jù),然后在函數(shù)中對其進行處理,將其使用html()來內(nèi)置id為serverResponse的文本值。
check.php
<span style="font-size:24px;color:#330000;"><?php $mark=0; $user=$_REQUEST['user']; $pwd=$_REQUEST['pwd']; $host='localhost'; $dbname='databaseweb'; $dsn = "mysql:host=$host;dbname=$dbname"; $root='root'; $mysql_pwd='數(shù)據(jù)庫密碼'; try { $pdo = new PDO($dsn, $root, $mysql_pwd); } catch(PDOException $e) { echo "數(shù)據(jù)庫連接失敗"; } $row_column= $pdo->query("select * from user where name='$user' and password='$pwd' "); //$row_column是從數(shù)據(jù)庫傳回的list(rows),不能做布爾型的判斷。 //但是我們可以通過list中的object元素的個數(shù)來做布爾判斷其中fetchColumn()函數(shù)用于獲取row的個數(shù) if($row_column->fetchColumn() >0 ) { echo "匹配成功!"; return; } else { echo "用戶".$user."不存在"; return; } ?></span>
就check.php文件而言,就是使用了PDO代替了Mysqli而已,但是這是處理數(shù)據(jù)庫的趨勢。學(xué)學(xué)吧!
對了一直沒上自己界面的圖片,這次補上。
登陸界面:
賬戶密碼為空 alert提醒:
賬戶密碼與數(shù)據(jù)庫異步匹配無誤:
賬戶密碼與數(shù)據(jù)庫匹配有誤:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jquery實現(xiàn)界面無刷新加載登陸注冊
- struts2+jquery實現(xiàn)ajax登陸實例詳解
- jQuery的cookie插件實現(xiàn)保存用戶登陸信息
- 使用jQuery插件創(chuàng)建常規(guī)模態(tài)窗口登陸效果
- jQuery登陸判斷簡單實現(xiàn)代碼
- 將jQuery應(yīng)用于login頁面的問題及解決
- Jsp中解決session過期跳轉(zhuǎn)到登陸頁面并跳出iframe框架的方法
- httpclient模擬登陸具體實現(xiàn)(使用js設(shè)置cookie)
- ExtJs 表單提交登陸實現(xiàn)代碼
- JS簡單實現(xiàn)登陸驗證附效果圖
相關(guān)文章
IE中jquery.form中ajax提交沒反應(yīng)解決方法分享
用jquery form插件,進行ajax提交,本來可以用,好好地,突然發(fā)現(xiàn),firefox,opera等可以提交,ie的success函數(shù)運行了2012-09-09完美兼容各大瀏覽器的jQuery仿新浪圖文淡入淡出間歇滾動特效
本文是作者學(xué)習(xí)jQuery之后練手之作,兼容各大瀏覽器,非常的精美實用,這里放出來給小伙伴們,有需要的直接拿走,別跟我客氣^_^2014-11-11BootStrap table表格插件自適應(yīng)固定表頭(超好用)
這篇文章主要介紹了BootStrap table表格插件自適應(yīng)固定表頭(超好用)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08jQuery向上遍歷DOM樹之parents(),parent(),closest()之間的區(qū)別
這篇文章主要是對jQuery向上遍歷DOM樹之parents(),parent(),closest()之間的區(qū)別進行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12