Home > php教程 > php手册 > body text

无刷新删除数据

WBOY
Release: 2016-06-13 10:33:59
Original
960 people have browsed it

首先本例基于留言本整理版修改。

 

我们使用了jquery.js来实现ajax和dom删除

首先加入

给table加个 id="t"

 

写个js:

<script></script>

function delItem (id) {

$.get(delete.php?id=+id,null,function (msg) {//ajax请求,请求后执行下面代码

if (1==msg) {//返回1表示成功

$(#t+id).remove();//把id为txx 的表格删除

} else {//否则弹出错误信息

alert(msg);

}

 

});

}

 

删除链接改成 href="javascript:delItem()"

 

delete.php的修改就是把错误语句改成直接输出就行了。

 

OK完成。

 

index.tpl :  

  

 

 

 

 

所有留言  

 

  

 

 

 

 

Welcome:  

退出  

 

登录  

注册  

 

发表留言  

 

 

  

 

    

 

  

 

  

 

    

 

  

 

  

 

    

 

  

 

[] 发表于:
 

    
 

 

    附件:  

 

修改 删除
 

 

 

<script> </script>

function delItem (id) {  

    $.get(delete.php?id=+id,null,function (msg) {  

        if (1==msg) {  

            $(#t+id).remove();  

        } else {  

            alert(msg);  

        }  

      

    });  

}  

 

 

 

  

delete.php :  

  

require(common.php);  

// 查询出留言信息  

$q = $query->query(select * from gb_content where id=.intval($_GET[id]));  

$rs = $query->fetch_array($q);  

$error = array();  

if ($rs[user_id]!=intval($_SESSION[user_id])) {// 判断user_id是否相同  

    $error = 该信息你不能删除,只能删除自己发布的;  

}  

if (!$error) {  

    $query->query(delete from gb_content where id=.intval($_GET[id]));//删除语句  

    if ($rs[user_file]) {//删除附件  

        @unlink(uploads/.$rs[user_file]);  

    }  

    echo 1;//表示成功  

} else {  

    echo $error;  

}  

?>  

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!