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 相关文章推荐
PHP出错界面
Oct 09 PHP
一段防盗连的PHP代码
Dec 06 PHP
开发大型 PHP 项目的方法
Jan 02 PHP
《PHP编程最快明白》第二讲 数字、浮点、布尔型、字符串和数组
Nov 01 PHP
PHP表单递交控件名称含有点号(.)会被转化为下划线(_)的处理方法
Jan 06 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
Jun 20 PHP
php读取torrent种子文件内容的方法(测试可用)
May 03 PHP
php中实现进程锁与多进程的方法
Sep 18 PHP
PHP文字转图片功能原理与实现方法分析
Aug 31 PHP
php微信公众号开发之秒杀
Oct 20 PHP
laravel5.5添加echarts实现画图功能的方法
Oct 09 PHP
php7 错误处理机制修改实例分析
May 25 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
mysql 中InnoDB和MyISAM的区别分析小结
2008/04/15 PHP
dedecms中使用php语句指南
2014/11/13 PHP
Laravel模型事件的实现原理详解
2018/03/14 PHP
laravel中短信发送验证码的实现方法
2018/04/25 PHP
Yii框架getter与setter方法功能与用法分析
2019/10/22 PHP
JS中confirm,alert,prompt函数区别分析
2011/01/17 Javascript
jQueryUI写一个调整分类的拖放效果实现代码
2012/05/10 Javascript
SwfUpload在IE10上不出现上传按钮的解决方法
2013/06/25 Javascript
改变文件域的样式实现思路同时兼容ie、firefox
2013/10/23 Javascript
两种方法基于jQuery实现IE浏览器兼容placeholder效果
2014/10/14 Javascript
JavaScript与HTML的结合方法详解
2015/11/23 Javascript
BootStrap tab选项卡使用小结
2020/08/09 Javascript
Node.js的Mongodb使用实例
2016/12/30 Javascript
jQuery实现鼠标悬停3d菜单展开动画效果
2017/01/19 Javascript
jQuery开源组件BootstrapValidator使用详解
2017/06/29 jQuery
js使用highlight.js高亮你的代码
2017/08/18 Javascript
基于node.js之调试器详解
2017/08/22 Javascript
浅谈angular.copy() 深拷贝
2017/09/14 Javascript
手把手教你使用vue-cli脚手架(图文解析)
2017/11/08 Javascript
详解vue.js之props传递参数
2017/12/12 Javascript
Vue数据双向绑定的深入探究
2018/11/27 Javascript
vue双向绑定及观察者模式详解
2019/03/19 Javascript
解决vue 子组件修改父组件传来的props值报错问题
2019/11/09 Javascript
用python实现面向对像的ASP程序实例
2014/11/10 Python
解决keras模型保存h5文件提示无此目录问题
2020/07/01 Python
Python如何将模块打包并发布
2020/08/30 Python
python实现杨辉三角的几种方法代码实例
2021/03/02 Python
HTML5添加鼠标悬浮音响效果不使用FLASH
2014/04/23 HTML / CSS
HTML5中原生的右键菜单创建方法
2016/06/28 HTML / CSS
如果重写了对象的equals()方法,需要考虑什么
2014/11/02 面试题
2014年便民服务中心工作总结
2014/12/20 职场文书
全国爱牙日活动总结
2015/02/05 职场文书
会计出纳岗位职责
2015/03/31 职场文书
2015年保管员工作总结
2015/04/30 职场文书
深度学习详解之初试机器学习
2021/04/14 Python
MySQL Threads_running飙升与慢查询的相关问题解决
2021/05/08 MySQL