Home > php教程 > php手册 > PHP+ajax 无刷新删除数据

PHP+ajax 无刷新删除数据

WBOY
Release: 2016-06-06 20:32:57
Original
1417 people have browsed it

首先本例基于留言本整理版修改。无刷新删除数据 (PHP)我们使用了jquery.js来实现ajax和dom删除

首先本例基于留言本整理版修改。
我们使用了jquery.js来实现ajax和dom删除
首先加入
代码如下:


给table加个
代码如下:
id="t"

写个js:
代码如下:
<script> <BR>function delItem (id) { <BR>$.get('delete.php?id='+id,null,function (msg) {//ajax请求,请求后执行下面代码 <BR>if ('1'==msg) {//返回1表示成功 <BR>$('#t'+id).remove();//把id为txx 的表格删除 <BR>} else {//否则弹出错误信息 <BR>alert(msg); <BR>} <BR>}); <BR>} <BR></script>

删除链接改成 href="javascript:delItem('')"
delete.php的修改就是把错误语句改成直接输出就行了。
OK完成。
index.tpl :
代码如下:




所有留言





Welcome:
退出

登录
注册

发表留言











[] 发表于:




附件:

修改 删除



<script> <BR>function delItem (id) { <BR>$.get('delete.php?id='+id,null,function (msg) { <BR>if ('1'==msg) { <BR>$('#t'+id).remove(); <BR>} else { <BR>alert(msg); <BR>} <BR>}); <BR>} <BR></script>



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;
}
?>
Related labels:
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