当前位置: 首页 > 图文教程 > 网络编程 > PHP > 用JQuery和PHP实现无刷新删除数据库数据

PHP
php 服务器调试 Zend Debugger 的安装教程
从Web查询数据库之PHP与MySQL篇
php 应用程序安全防范技术研究
php 不同编码下的字符串长度区分
php 生成饼图 三维饼图
PHP 字符截取 解决中文的截取问题,不用mb系列
PHP5 操作MySQL数据库基础代码
php面向对象全攻略 (一) 面向对象基础知识
php面向对象全攻略 (二) 实例化对象 使用对象成员
php面向对象全攻略 (三)特殊的引用“$this”的使用
php面向对象全攻略 (四)构造方法与析构方法
php面向对象全攻略 (五) 封装性
php面向对象全攻略 (六)__set() __get() __isset() __unset()的用法
php面向对象全攻略 (七) 继承性
php面向对象全攻略 (八)重载新的方法
php面向对象全攻略 (九)访问类型
php面向对象全攻略 (十) final static const关键字的使用
php面向对象全攻略 (十一)__toString()用法 克隆对象 __call处理调用错误
php面向对象全攻略 (十二) 抽象方法和抽象类
php面向对象全攻略 (十四) php5接口技术

用JQuery和PHP实现无刷新删除数据库数据


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-03-17   浏览: 490 ::
收藏到网摘: n/a

利用JQuery与PHP实现无刷新删除数据库数据,这个小功能一共需要三个文件,一个JS,两个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;

  }

  ?>