jquery + ajax +smarty+php教程 无刷新删除数据代码
写个js:view sourceprint?01
02 function delItem (id) {
03 $.get('delete.php?id='+id,null,function (msg) {//ajax请求,请求后执行下面<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%B4%FA%C2%EB">代码</SPAN>
04 if ('1'==msg) {//返回1表示成功
05 $('#t'+id).remove();//把id为txx 的表格删除
06 } else {//否则弹出错误信息
07 alert(msg);
08 }
09
10 });
11 }
删除链接改成 href="javascript教程:delItem('<!--{$item.id}-->')" delete.php的修改就是把错误语句改成直接输出就行了。 OK完成。 index.tpl :view sourceprint?01 <!DOCTYPE <SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=html">html</SPAN> PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02 <html <SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=xml">xml</SPAN>ns="http://www.w3.org/1999/xhtml">
03 <head>
04 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
05 <title>所有留言</title>
06 <link rel="stylesheet" type="text/css教程" href="style.css" media="all" />
07
08 <script type="text/javascript" src="lib/jquery.js"></script>
09 </head>
10 <body>
11 <!--{if $smarty.<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=session">session</SPAN>.username}-->
12 Welcome:<!--{$smarty.session.username}-->
13 <a href="logout.php">退出</a>
14 <!--{else}-->
15 <a href="login.php">登录</a>
16 <a href="reg.php">注册</a>
17 <!--{/if}-->
18 <a href="add.php">发表留言</a>
19 <!--{foreach from=$gblist item=item}-->
20 <table id="t<!--{$item.id}-->" width="700" border="0" cellspacing="0" cellpadding="0" class="tb">
21 <tr>
22 <td class="bg"><b>[<!--{$item.username}-->]</b> 发表于:<!--{$item.insert_time}--></td>
23 </tr>
24 <tr>
25 <td><!--{$item.content}-->
26 <br />
27 <!--{if $item.user_file}-->
28 附件:<a target="_blank" href="uploads/<!--{$item.user_file}-->"><!--{$item.user_file}--></a>
29 <!--{/if}-->
30 </td>
31 </tr>
32 <tr>
33 <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>
34 </tr>
35 </table>
36 <!--{/foreach}-->
37 <!--{$pagePanel}-->
38 <script>
39 function delItem (id) {
40 $.get('delete.php?id='+id,null,function (msg) {
41 if ('1'==msg) {
42 $('#t'+id).remove();
43 } else {
44 alert(msg);
45 }
46
47 });
48 }
49 </script>
50 </body>
51 </html>
delete.php :view sourceprint?01 <?php
02 require('common.php');
03 // <SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%B2%E9%D1%AF">查询</SPAN>出留言信息
04 $q = $query->query('select * from gb_content where id='.intval($_GET['id']));
05 $rs = $query->fetch_array($q);
06 $error = array();
07 if ($rs['user_id']!=intval($_SESSION['user_id'])) {// 判断user_id是否相同
08 $error = '该信息你不能删除,只能删除自己发布的';
09 }
10 if (!$error) {
11 $query->query('delete from gb_content where id='.intval($_GET['id']));//删除语句
12 if ($rs['user_file']) {//删除附件
13 @unlink('uploads/'.$rs['user_file']);
14 }
15 echo 1;//表示成功
16 } else {
17 echo $error;
18 }
19 ?>