Ajax應(yīng)用:使用jQuery和PHP實(shí)現(xiàn)功能開關(guān)效果
在開發(fā)項目中,我們會經(jīng)常碰到需要及時開啟某項功能的情況,通過Ajax實(shí)現(xiàn)實(shí)時開啟和關(guān)閉功能,無疑增強(qiáng)了用戶體驗(yàn)。本文以360安全衛(wèi)士的木馬防火墻開關(guān)為背景,使用PHP、jquery、MYSQL實(shí)現(xiàn)了及時開啟和關(guān)閉產(chǎn)品功能的WEB應(yīng)用。

準(zhǔn)備工作為了更好的演示本例,我們需要一個數(shù)據(jù)表,記錄需要的功能說明及開啟狀態(tài),表結(jié)構(gòu)如下:
- 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
我們要在頁面顯示相關(guān)功能列表,使用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)) {
- ?>
- <div class="list">
- <div class="fun_title">
- <span rel="<?php echo $row['id'];?>" <?php if($row['status']==1){ ?>
- class="ad_on" title="點(diǎn)擊關(guān)閉"<?php }else{?>class="ad_off" title="點(diǎn)擊開啟"<?php }?>></span>
- <h3><?php echo $row['title']; ?></h3>
- </div>
- <p><?php echo $row['description'];?></p>
- </div>
- <?php } ?>
連接數(shù)據(jù)庫,然后循環(huán)輸出產(chǎn)品功能列表。
CSS
為了渲染一個比較好的頁面外觀,我們使用CSS來美化頁面,使得頁面更符合人性化。使用CSS,我們只需用一張圖片來標(biāo)識開關(guān)按鈕。

- .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
我們通過單擊開關(guān)按鈕,及時請求后臺,改變對應(yīng)的功能開關(guān)狀態(tài)。這個過程是一個典型的Ajax應(yīng)用。通過點(diǎn)擊開關(guān)按鈕,前端向后臺PHP發(fā)送post請求,后臺接收請求,并查詢數(shù)據(jù)庫,并將結(jié)果返回給前端,前端jQuery根據(jù)后臺返回的結(jié)果,改變按鈕狀態(tài)。
- $(function(){
- //鼠標(biāo)滑向換色
- $(".list").hover(function(){
- $(this).addClass("cur_select");
- },function(){
- $(this).removeClass("cur_select");
- });
- //關(guān)閉
- $(".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","點(diǎn)擊開啟");
- }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","點(diǎn)擊關(guān)閉");
- }else{
- alert(data);
- }
- });
- });
- });
說明,代碼中,首先實(shí)現(xiàn)了鼠標(biāo)滑向功能列表換色的功能(詳見demo),然后就是單擊開關(guān)按鈕,向后臺action.php發(fā)送Ajax請求,提交的參數(shù)是對應(yīng)功能的id和type,用于后臺區(qū)分請求的是哪個功能和請求的類型(開啟和關(guān)閉)。其實(shí),大家稍微留神,可以看出,根據(jù)Ajax請求成功返回結(jié)果后,開關(guān)按鈕動態(tài)改變樣式,實(shí)現(xiàn)改變開關(guān)狀態(tài)的功能。
action.php
后臺action.php接收到前端的請求,根據(jù)參數(shù)執(zhí)行SQL語句,更新對應(yīng)功能的狀態(tài),成功后將結(jié)果返回給前端,請看代碼:
- require_once('connect.php');
- $id = $_POST['status'];
- $type = $_POST['type'];
- if($type==1){ //關(guān)閉
- $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 '服務(wù)器忙,請稍后再試!';
- }
結(jié)束語通過本文您可以熟練掌握ajax在WEB開發(fā)中的應(yīng)用,并能快速的應(yīng)用到您的項目中。helloweba將一如既往的為廣大開發(fā)者提供更具實(shí)用性的應(yīng)用,致力于WEB前端技術(shù)的應(yīng)用。
原文:http://www.helloweba.com/view-blog-153.html
【編輯推薦】