Ajax+PHP实现的删除数据功能示例


Posted in PHP onFebruary 12, 2019

本文实例讲述了Ajax+PHP实现的删除数据功能。分享给大家供大家参考,具体如下:

一 代码

conn.php:

<?php
  $conn = mysql_connect("localhost", "root", " ") or die("连接数据库服务器失败!".mysql_error()); //连接MySQL服务器
  mysql_select_db("db_database27",$conn); //选择数据库db_database27
  mysql_query("set names utf8"); //设置数据库编码格式utf8
?>

del.php:

<?php
  include_once("conn/conn.php");//包含数据库连接文件
    $id=$_GET['id'];//把传过来的参数值赋给变量$i
    $sql=mysql_query("delete from tb_demo02 where id=".$id);//根据参数值执行相应的删除操作
    if($sql){//如果操作的返回值为true
     $reback=1;//把变量$reback的值设为1
    }else{
     $reback=0;//否则变量$reback的值设为0
    }
    echo $reback;//输出变量$reback的值
?>

index.js:

function del(id){
    var xml;
    if(window.ActiveXObject){//如果是浏览器支持ActiveXObjext则创建ActiveXObject对象
     xml=new ActiveXObject('Microsoft.XMLHTTP');
    }else if(window.XMLHttpRequest){//如果浏览器支持XMLHttpRequest对象则创建XMLHttpRequest对象
     xml=new XMLHttpRequest();
    }
    xml.open("GET","del.php?id="+id,true);//使用GET方法调用del.php并传递参数的值
    xml.onreadystatechange=function(){//当服务器准备就绪执行回调函数
     if(xml.readystate==4 && xml.status==200){//如果服务器已经传回信息并未发生错误
        var msg=xml.responseText;//把服务器传回的值赋给变量msg
        if(msg==1){//如果服务器传回的值为1则提示删除成功
         alert("删除成功!");
      location.reload();
        }else{//否则提示删除失败
         alert("删除失败!");
         return false;
        }
   }
    }
    xml.send(null);//不发送任何数据,因为数据已经使用请求URL通过GET方法发送
}

index.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>分组统计</title>
<link rel="stylesheet" type="text/css" href="mystyle.css" rel="external nofollow" >
</head>
<body>
<script type="text/javascript" src="index.js"></script>
<center>
<!--banner-->
<table width="798" border="0" cellpadding="0" cellspacing="0">
  <tr>
      <td height="112" background="images/banner.jpg"></td>
  </tr>
</table>
<?php
include_once("conn/conn.php");
?>
<table width="780" border="0" cellpadding="0" cellspacing="0">
<form name="form1" id="form1" method="post" action="deletes.php">
 <tr>
     <td height="20" width="5%" class="top"> </td>
  <td width="5%" class="top">id</td>
  <td width="30%" class="top">书名</td>
  <td width="10%" class="top">价格</td>
  <td width="20%" class="top">出版时间</td>
  <td width="10%" class="top">类别</td>
    <td width="10%" class="top">操作</td>
 </tr>
<?php
    $sqlstr1 = "select * from tb_demo02 order by id";//按id的升序查询表tb_demo02的数据
    $result = mysql_query($sqlstr1,$conn);//执行查询语句
    while ($rows = mysql_fetch_array($result)){//循环输出查询结果
?>
 <tr>
  <td height="25" align="center" class="m_td">
    <input type=checkbox name="chk[]" id="chk" value=".$rows['id'].">
    </td>
    <td height="25" align="center" class="m_td"><?php echo $rows['id'];?></td>
    <td height="25" align="center" class="m_td"><?php echo $rows['bookname'];?></td>
  <td height="25" align="center" class="m_td"><?php echo $rows['price'];?></td>
    <td height="25" align="center" class="m_td"><?php echo $rows['f_time'];?></td>
    <td height="25" align="center" class="m_td"><?php echo $rows['type'];?></td>
    <td class="m_td"><a href="#" rel="external nofollow" onClick="del(<?php echo $rows['id'];?>)">删除</a></td>
 </tr>
<?php
    }
?>
<tr>
    <td height="25" colspan="7" class="m_td" align="left">  </td>
</tr>
</form>
</table>
<!--show-->
 <table width="798" border="0" cellpadding="0" cellspacing="0">
  <tr>
   <td height="48" background="images/bottom.jpg"> </td>
  </tr>
</table>
</center>
</body>
</html>

二 运行结果

Ajax+PHP实现的删除数据功能示例

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
输出控制类
Oct 09 PHP
解析php file_exists无效的解决办法
Jun 26 PHP
PHP中的多行字符串传递给JavaScript的两种方法
Jun 19 PHP
smarty中js的调用方法示例
Oct 27 PHP
php动态生成缩略图并输出显示的方法
Apr 20 PHP
PHP扩展迁移为PHP7扩展兼容性问题记录
Feb 15 PHP
php使用curl通过代理获取数据的实现方法
May 16 PHP
PHP线程的内存回收问题
Jul 08 PHP
浅谈php中curl、fsockopen的应用
Dec 10 PHP
PHP中Notice错误常见解决方法
Apr 28 PHP
PHPTree――php快速生成无限级分类
Mar 30 PHP
PHP变量的作用范围实例讲解
Dec 22 PHP
PHP chr()函数讲解
Feb 11 #PHP
PHP chop()函数讲解
Feb 11 #PHP
PHP bin2hex()函数基础实例讲解
Feb 11 #PHP
Ajax+PHP实现的分类列表框功能示例
Feb 11 #PHP
Ajax+PHP实现的模拟进度条功能示例
Feb 11 #PHP
Ajax+Jpgraph实现的动态折线图功能示例
Feb 11 #PHP
php防止表单重复提交实例讲解
Feb 11 #PHP
You might like
php设计模式小结
2013/02/15 PHP
php解决约瑟夫环示例
2014/04/09 PHP
PHP fastcgi模式上传大文件(大约有300多K)报错
2014/09/28 PHP
php curl抓取网页的介绍和推广及使用CURL抓取淘宝页面集成方法
2015/11/30 PHP
深入理解Yii2.0乐观锁与悲观锁的原理与使用
2017/07/26 PHP
E3 tree 1.6在Firefox下显示问题的修复方法
2013/01/30 Javascript
js获取或设置当前窗口url参数的小例子
2013/10/14 Javascript
通过js简单实现将一个文本内容转译成加密文本
2013/10/22 Javascript
JS中的异常处理方法分享
2013/12/22 Javascript
JSON+HTML实现国家省市联动选择效果
2014/05/18 Javascript
jquery实现简单的二级导航下拉菜单效果
2015/09/07 Javascript
原生js实现addClass,removeClass,hasClass方法
2016/04/27 Javascript
原生Javascript和jQuery做轮播图简单例子
2016/10/11 Javascript
9个让JavaScript调试更简单的Console命令
2016/11/14 Javascript
Javascript 高性能之递归,迭代,查表法详解及实例
2017/01/08 Javascript
浅谈mvvm-simple双向绑定简单实现
2018/04/18 Javascript
vue组件横向树实现代码
2018/08/02 Javascript
JS实现把一个页面层数据传递到另一个页面的两种方式
2018/08/13 Javascript
全面了解JavaScript的作用域链
2019/04/03 Javascript
序列化模块json代码实例详解
2020/03/03 Javascript
Vue作用域插槽实现方法及作用详解
2020/07/08 Javascript
python调用Moxa PCOMM Lite通过串口Ymodem协议实现发送文件
2014/08/15 Python
Python中几个比较常见的名词解释
2015/07/04 Python
微信跳一跳小游戏python脚本
2018/01/05 Python
使用Python代码实现Linux中的ls遍历目录命令的实例代码
2019/09/07 Python
python+django+selenium搭建简易自动化测试
2020/08/19 Python
协程Python 中实现多任务耗资源最小的方式
2020/10/19 Python
python pyg2plot的原理知识点总结
2021/02/28 Python
女性时尚在线:IVRose
2019/02/23 全球购物
如何在存储过程中使用Loop
2016/01/05 面试题
会计毕业生自荐书
2014/06/12 职场文书
2014年度个人工作总结
2014/11/07 职场文书
大学生支教感言
2015/08/01 职场文书
超市员工管理制度
2015/08/06 职场文书
班主任远程培训研修日志
2015/11/13 职场文书
关于办理居住证的介绍信模板
2019/11/27 职场文书