PHP+ajax 无刷新删除数据

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

<script type="text/javascript" src="lib/jquery.js"></script>

给table加个
复制代码 代码如下:

id="t<!--{$item.id}-->"

写个js:
复制代码 代码如下:

<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);
}
});
}
</script>

删除链接改成 href="javascript:delItem('<!--{$item.id}-->')"
delete.php的修改就是把错误语句改成直接输出就行了。
OK完成。
index.tpl :
复制代码 代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>所有留言</title>
<link rel="stylesheet" type="text/css" href="style.css" media="all" />
<script type="text/javascript" src="lib/jquery.js"></script>
</head>
<body>
<!--{if $smarty.session.username}-->
Welcome:<!--{$smarty.session.username}-->
<a href="logout.php">退出</a>
<!--{else}-->
<a href="login.php">登录</a>
<a href="reg.php">注册</a>
<!--{/if}-->
<a href="add.php">发表留言</a>
<!--{foreach from=$gblist item=item}-->
<table id="t<!--{$item.id}-->" width="700" border="0" cellspacing="0" cellpadding="0" class="tb">
<tr>
<td class="bg"><b>[<!--{$item.username}-->]</b> 发表于:<!--{$item.insert_time}--></td>
</tr>
<tr>
<td><!--{$item.content}-->
<br />
<!--{if $item.user_file}-->
附件:<a target="_blank" href="uploads/<!--{$item.user_file}-->"><!--{$item.user_file}--></a>
<!--{/if}-->
</td>
</tr>
<tr>
<td align="right"><!--{if $item.user_id==$smarty.session.user_id}--><a href="add.php?id=<!--{$item.id}-->">修改</a> <a href="javascript:delItem('<!--{$item.id}-->')">删除</a><!--{/if}--></td>
</tr>
</table>
<!--{/foreach}-->
<!--{$pagePanel}-->
<script>
function delItem (id) {
$.get('delete.php?id='+id,null,function (msg) {
if ('1'==msg) {
$('#t'+id).remove();
} else {
alert(msg);
}
});
}
</script>
</body>
</html>

delete.php :
复制代码 代码如下:

<?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;
}
?>

相关文章

PHP 数组遍历方法大全(foreach,list,each)

在PHP中数组分为两类: 数字索引数组和关联数组。 其中数字索引数组和C语言中的数组一样,下标是为0,1,2… 而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。...

PHP以指定字段为索引返回数据库所取的数据数组

很多情况下,我们从接触一个新的项目到开发完成,再回过头来仔细浏览一下自己写的代码,很多都是我们以前用熟练的代码。所以,在完成每个新项目的时 候,适当的做些项目总结、代码总结,或许你会在以...

php编写简单的文章发布程序

-- -- 表的结构 `yi_article` -- CREATE TABLE IF NOT EXISTS `yi_article` ( `id` int(11) unsign...

php外部执行命令函数用法小结

本文实例讲述了php外部执行命令函数用法。分享给大家供大家参考,具体如下: 首先先要自我检讨一下的,学习和使用php已经前前后后一年多了,研究和使用Linux系统也差不多一年了,我居然不...

PHP Header用于页面跳转时的几个注意事项

前言 本文介绍的是在PHP中用header("location:test.php")进行跳转要注意以下几点,有助于解决一些新手经常遇到的问题 一、location和“:”号间不能有空格,...