ajax實(shí)現(xiàn)改變狀態(tài)和刪除無刷新的實(shí)例
1. 01.php為主程序,調(diào)用smarty模板遍歷輸出:
<?php include './include/Mysql.class.php'; include './libs/Smarty.class.php'; $db=new Mysql; $smarty=new Smarty; $lists=$db->getALL('users'); $smarty->assign('lists',$lists); $smarty->display('list.html'); ?>
2. list.html模板:內(nèi)容結(jié)合JS ajax使用:
<!DOCTYPE html> <html> <head> <meta charset=utf-8> <title>用戶權(quán)限展示表</title> </head> <body> //給table體設(shè)置一個(gè)div,方便js調(diào)用 <div id="table"> <table align="center" border="1" width="500"> <center><h2>用戶權(quán)限表</h2></center> <tr> <th>uid</th><th>用戶名</th><th>密碼</th><th>鎖定狀態(tài)</th><th>角色</th><th>操作</th> </tr> {foreach $lists as $list} <tr align="center"> <td>{$list.uid}</td> <td>{$list.username}</td> <td>{$list.password}</td> {if $list.is_lock==1} <td><a href="javascript:lock(0,{$list.uid});" rel="external nofollow" >鎖定</a></td> {else} <td><a href="javascript:lock(1,{$list.uid})" rel="external nofollow" ;>取消鎖定</a></td> {/if} {if $list.role==1} <td>管理員</td> {else} <td>編輯者</td> {/if} <td><a href="javascript:del({$list.uid})" rel="external nofollow" >刪除</a></td> </tr> {/foreach} </table> </div> </body> <script type="text/javascript"> function lock(lock,uid){ //創(chuàng)建ajax對(duì)象 var xhr=new XMLHttpRequest(); //打開一個(gè)鏈接 xhr.open('post','02.php'); //設(shè)置頭信息 xhr.setRequestHeader('content-type','application/x-www-form-urlencoded'); //取值,多個(gè)參數(shù)用&分開 var data="is_lock="+lock+"&uid="+uid; //發(fā)送ajax數(shù)據(jù)請(qǐng)求 xhr.send(data); //設(shè)置回調(diào)、監(jiān)聽函數(shù) xhr.onreadystatechange=function(){ //如果ajax狀態(tài)碼響應(yīng)正常且網(wǎng)絡(luò)正常,獲取響應(yīng)文本 if(xhr.readyState==4&&xhr.status==200){ if(xhr.responseText){ document.getElementById('table').innerHTML=xhr.responseText; }else{ alert("切換狀態(tài)失敗!"); } } } } function del(uid){ var del=window.confirm("您確定要?jiǎng)h除嗎?"); if(del){ //創(chuàng)建ajax對(duì)象 var xhr=new XMLHttpRequest(); //打開一個(gè)鏈接 xhr.open('post','del.php'); //設(shè)置header頭 xhr.setRequestHeader('content-type','application/x-www-form-urlencoded'); //data取值 var data="uid="+uid; //發(fā)送ajax請(qǐng)求 xhr.send(data); //設(shè)置監(jiān)聽 xhr.onreadystatechange=function(){ //如果ajax狀態(tài)碼響應(yīng)正常且網(wǎng)絡(luò)正常,獲取響應(yīng)文本 if(xhr.readyState==4&&xhr.status==200){ if(xhr.responseText){ //用ajax響應(yīng)內(nèi)容替換本模板中table標(biāo)簽的內(nèi)容 document.getElementById('table').innerHTML=xhr.responseText; }else{ alert("刪除失??!"); } } } } } </script> </html>
3. 02.php改變狀態(tài)無刷新:
<?php include './include/Mysql.class.php'; include './libs/Smarty.class.php'; $lock=$_POST['is_lock']; $uid=$_POST['uid']; $smarty=new Smarty; $db=new Mysql; $result=$db->update('users',"is_lock=$lock","uid=$uid"); if($result){ //修改成功重新遍歷數(shù)據(jù)庫(kù)并輸出smarty模板 $lists=$db->getALL('users'); $smarty->assign('lists',$lists); $smarty->display('list.html'); }else{ echo false; } ?>
4.del.php實(shí)現(xiàn)刪除無刷新
<?php include './include/Mysql.class.php'; include './libs/Smarty.class.php'; $db=new Mysql; $smarty=new Smarty; $uid=$_POST['uid']; $res=$db->delete('users',$uid); if($res>0){ $lists=$db->getALL('users'); $smarty->assign('lists',$lists); $smarty->display('list.html'); }else{ echo false; } ?>
以上這篇ajax實(shí)現(xiàn)改變狀態(tài)和刪除無刷新的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
AJAX解析XML實(shí)例之下拉框省、市二級(jí)聯(lián)動(dòng)
實(shí)現(xiàn)省、市二級(jí)聯(lián)動(dòng),當(dāng)選擇某一省時(shí),改省下面的市就會(huì)在另一個(gè)下拉框顯示出來,下面有個(gè)不錯(cuò)的示例,需要的朋友可以參考下2013-11-11ajax實(shí)現(xiàn)提交時(shí)校驗(yàn)表單方法
這篇文章主要為大家詳細(xì)介紹了ajax實(shí)現(xiàn)提交時(shí)校驗(yàn)表單方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02ajax實(shí)現(xiàn)select三級(jí)聯(lián)動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了ajax動(dòng)態(tài)實(shí)現(xiàn)select三級(jí)聯(lián)動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Ajax獲取數(shù)據(jù)然后顯示在頁(yè)面的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狝jax獲取數(shù)據(jù)然后顯示在頁(yè)面的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08Jquery基于Ajax方法自定義無刷新提交表單Form實(shí)例
這篇文章主要介紹了Jquery基于Ajax方法自定義無刷新提交表單Form的方法,結(jié)合實(shí)例詳細(xì)分析了Ajax無刷新提交表單的完整實(shí)現(xiàn)過程,并總結(jié)了使用中的注意事項(xiàng),具有很好的借鑒價(jià)值,需要的朋友可以參考下2014-11-11強(qiáng)烈推薦 - Ajax 技術(shù)資源中心
強(qiáng)烈推薦 - Ajax 技術(shù)資源中心...2007-05-05Ajax實(shí)現(xiàn)靜態(tài)刷新頁(yè)面過程帶加載旋轉(zhuǎn)圖片
當(dāng)然你也可以使用jquery或者ajax寫動(dòng)態(tài)加載table的方式來實(shí)現(xiàn),方式多種多樣,感興趣的朋友可以了解下,希望本文提供的靜態(tài)刷新頁(yè)面對(duì)你學(xué)習(xí)ajax有所幫助2013-02-02非常簡(jiǎn)單的Ajax請(qǐng)求實(shí)例附源碼
這篇文章為大家推薦了一個(gè)非常簡(jiǎn)單的Ajax請(qǐng)求實(shí)例,可以在不重載頁(yè)面的情況與 Web 服務(wù)器交換數(shù)據(jù),感興趣的小伙伴們可以參考一下2015-11-11