jQuery和PHP打造功能開關效果
在開發(fā)項目中,我們會經(jīng)常碰到需要及時開啟某項功能的情況,通過Ajax實現(xiàn)實時開啟和關閉功能,無疑增強了用戶體驗。本文以360安全衛(wèi)士的木馬防火墻開關為背景,使用PHP、jquery、MYSQL實現(xiàn)了及時開啟和關閉產(chǎn)品功能的WEB應用。
準備工作
為了更好的演示本例,我們需要一個數(shù)據(jù)表,記錄需要的功能說明及開啟狀態(tài),表結構如下:
- CREATE TABLE `pro` (
- `id` int(11) NOT NULL auto_increment,
- `title` varchar(50) NOT NULL,
- `description` varchar(200) NOT NULL,
- `status` tinyint(1) NOT NULL default '0',
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
你可以向表中pro插入幾條數(shù)據(jù)。
index.php
我們要在頁面顯示相關功能列表,使用PHP讀取數(shù)據(jù)表,并以列表的形式展示。
- <?php
- require_once('connect.php'); //連接數(shù)據(jù)庫
- $query=mysql_query("select * from pro order by id asc");
- while ($row=mysql_fetch_array($query)) {
- ?>
- < class="list">
- < class="fun_title">
- <span rel="<?php echo $row['id'];?>" <?php if($row['status']==1){ ?>
- class="ad_on" title="點擊關閉"<?php }else{?>class="ad_off" title="點擊開啟"<?php }?>></span>
- <h3><?php echo $row['title']; ?></h3>
- </>
- <p><?php echo $row['description'];?></p>
- </>
- <?php } ?>
連接數(shù)據(jù)庫,然后循環(huán)輸出產(chǎn)品功能列表。
CSS
為了渲染一個比較好的頁面外觀,我們使用CSS來美化頁面,使得頁面更符合人性化。使用CSS,我們只需用一張圖片來標識開關按鈕。
- .list{padding:6px 4px; border-bottom:1px dotted #d3d3d3; position:relative}
- .fun_title{height:28px; line-height:28px}
- .fun_title span{width:82px; height:25px; background:url(switch.gif) no-repeat;
- cursor:pointer; position:absolute; right:6px; top:16px}
- .fun_title span.ad_on{background-position:0 -2px}
- .fun_title span.ad_off{background-position:0 -38px}
- .fun_title h3{font-size:14px; font-family:'microsoft yahei';}
- .list p{line-height:20px}
- .list p span{color:#f60}
- .cur_select{background:#ffc}
CSS代碼,我不想詳述,提示下我們使用了一張圖片,然后通過background-position來定位圖片的位置,這是大多數(shù)網(wǎng)站使用的方法,好處咱就不說了。
jQuery
我們通過單擊開關按鈕,及時請求后臺,改變對應的功能開關狀態(tài)。這個過程是一個典型的Ajax應用。通過點擊開關按鈕,前端向后臺PHP發(fā)送post請求,后臺接收請求,并查詢數(shù)據(jù)庫,并將結果返回給前端,前端jQuery根據(jù)后臺返回的結果,改變按鈕狀態(tài)。
- $(function(){
- //鼠標滑向換色
- $(".list").hover(function(){
- $(this).addClass("cur_select");
- },function(){
- $(this).removeClass("cur_select");
- });
- //關閉
- $(".ad_on").live("click",function(){
- var add_on = $(this);
- var status_id = $(this).attr("rel");
- $.post("action.php",{status:status_id,type:1},function(data){
- if(data==1){
- add_on.removeClass("ad_on").addClass("ad_off").attr("title","點擊開啟");
- }else{
- alert(data);
- }
- });
- });
- //開啟
- $(".ad_off").live("click",function(){
- var add_off = $(this);
- var status_id = $(this).attr("rel");
- $.post("action.php",{status:status_id,type:2},function(data){alert(data);
- if(data==1){
- add_off.removeClass("ad_off").addClass("ad_on").attr("title","點擊關閉");
- }else{
- alert(data);
- }
- });
- });
- });
說明,代碼中,首先實現(xiàn)了鼠標滑向功能列表換色的功能(詳見demo),然后就是單擊開關按鈕,向后臺action.php發(fā)送Ajax請求,提交的參數(shù)是對應功能的id和type,用于后臺區(qū)分請求的是哪個功能和請求的類型(開啟和關閉)。其實,大家稍微留神,可以看出,根據(jù)Ajax請求成功返回結果后,開關按鈕動態(tài)改變樣式,實現(xiàn)改變開關狀態(tài)的功能。
action.php
后臺action.php接收到前端的請求,根據(jù)參數(shù)執(zhí)行SQL語句,更新對應功能的狀態(tài),成功后將結果返回給前端,請看代碼:
- require_once('connect.php');
- $id = $_POST['status'];
- $type = $_POST['type'];
- if($type==1){ //關閉
- $sql = "update pro set status=0 where id=".$id;
- }else{ //開啟
- $sql = "update pro set status=1 where id=".$id;
- }
- $rs = mysql_query($sql);
- if($rs){
- echo '1';
- }else{
- echo '服務器忙,請稍后再試!';
- }