PHP怎结合ajax实现交互操作数据库删除数据
Admin 2022-07-08 群英技术资讯 833 次浏览
本文实例讲述了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>
二 运行结果
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PHP 5 的使用者可以使用 mysql extension,mysqli 和 PDO_MYSQL 。PHP 7移除了mysql extension,只剩下后面两种选择. 这份文档解释了每个API 的术语,帮助我们如何使用API 和了解相关API的信息。
在之前的文章《PHP函数运用之:利用数组来输出文件的扩展名》中,我们介绍了两种利用数组来获取文件扩展名的方法。这次我们继续介绍获取文件扩展名的方法,看看利用字符串函数要怎么操作,感兴趣的朋友可以学习了解一下~
php laravel框架自带命令的实现:1、作为服务提供者加载到程序中,laravel自带的artisan命令;2、然后找到 Up/Down命令入口;3、DownCommand实现,关键在当前存储目录/framework 下面创建。
PHP如何编写图形计算器?对四则运算计算器我们都很熟悉了,有些朋友就好奇能不能对图像计算呢?当然是可以的,下面就给大家介绍一下如何实现三角形,矩形和圆形计算的简易计算器
PHP自动加载类的原理是什么?有些朋友对在PHP 5中类的自动加载的原理不是很清楚,因此这篇文章就给大家介绍一下PHP自动加载类的基本概念,原理以及用法等等,感兴趣的朋友可以看一下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008