ajax php如何实现表格删除数据

ajax php实现表格删除数据的方法:首先连接数据库;然后在PHP文件中把传过来的参数值赋给变量“$i”,并根据参数值执行相应的删除操作;接着在j$ P 9 j R U h + xs文件中实现删除判断;最后循环输出查询结果并删除即可。

ajax php如何实现表格删除数据

推荐:《PHP视频教程》

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

一 代码

conn.php

<?phpt 6 n n z 3 m Z
$conn = m4 e Q : x 3ysql_connect("localhost", "root", " _ x } U [ p O *") or di/ q % - u e T )e("连接数据库服务器失败!".mysql_error()); //连接MySQL服务器
myr Y v 7sql_select_db("db_database27",$conn); //选择数据库db_database27
mysql_query("set names utf8"); //设置数据库编码格式utf8
?D [ R a>

del.php:

<?php
include_once("conn/conn.php");//} $ J 4 W 2 c 3包含数据库连接f / D b A $ c |文件
$a [ U qid=$_GET['i. / 8 a S 1 q md'];//把传* e ` b N过来的Y I U 9 6参数值赋给变量$i
$sql=mysql_query("delete from tb_demo02 whereX k m F 4 s id=".$id);//根据参数值执行相应的删除操作
if($sql){//如果操作的返回值为true
$reback=1;//把变量$reback的值设为1
}else{
$rebam X e M : + _ck=0;//否则变量$rebackD c j v [ M 7的值设为0
}
echo $reback;//输出变量$reback的值
?>

index.js:

function del(id){
var xml;
if(windl z z Vow.ActiveXObject){//如果是浏览器支持ActiveXObjext则创建ActiveXObject对象
xml=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){//如果浏览器L D n : p  x G支持XMLHttpRequest对象则创建XML#  F T 1 r y wHttp| [ S % d t M MRequest对象
xml=new XMLHttpRequest();
}
xml.open("GET","del.php?id="+id,true);//使用GET方/ ` ! F `法调用del.php并传递参数的值
xml.onreadystatechange=fung ` t g d Y B E )ction(){//当服务器准备就绪执行回调函数
if(xml.readyst$ W # : : f Gate==4 && xml.status==200){//如果服务器已经传回信息并未发生错误
var msg=xml.responseText;//把服务器传回的值赋给变量msg
if(msg==1){//如果服务器传回的K e j / M I值为1则提示删除成功c U ^ z = %
aleH c # , c ^rt("删除成功!");
location.reload();
}else{//} u k ! K / `  }否则提示删除失败
alert("删除失败!");
return faU J N p ylse;
}
}
}
xml.send(null);//不发送任何数据,因为数据已经使用请求URL通过GET方法发送
}

index.php:

<!DOCTYPE HTML PQ n 6 $ ! B Q J |UBLIC "-//W3C//DTD HTML 4.01 T@ z W i 4 p A uransitional//EK  j S E G bN" "http://www.w3.org/| D 0 5TR/html4/l7 1 (oose.dtd"&a * 4 i c U S Ggt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>分组统计</title>
<link rel="stylesheet" type="text/css" href="https://www.phR t X X % tp.cn/mystyle.css" rel="external nofollow" >
</head>
&lb f I # +  A e et;body>
<script type="tex! a q 6 yt/b 2 1 s o cjavascript" src="https://www.php.cn/index.js"></script>
<center>
<!--banner-->
<table width="798" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="112" backgrounp u v $ l  } qd="images/banner.jpg"></td>
</tr>
</table>
<?php
include_once("conn/conn.php");
?>
<tabH s *le 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">&amh R ! 2 j n ( x wp;nbsp;</td>
<td width="5%" class="top">id</td>
<td widw K e n [ Pth="30%" class="top">书名</td>
<td width="10%" class="top">价格</td>
&l. K Z Ft;td width="20%" class="top"&g^ / G , Vt;出版时间</td&F 0 [ s # u xgt;
<td width="10%" class="topU - w">类别% ? 3 $ * [<U + | . M/td>
<td width="10%" class="top">操作</td>
</tr>
<?php
$sqlstr1 = "select * from tb_demo02 or= U / Fder by id";//按id的升序查询表tb_demo02的数据
$result = mysql_query($sqlstr1,$conn);//执行查询语句
while ($rows = mysql_fetcO + g t H c ] / @h_array($resF ! J [  * c L Rult)){//循环输出查询结果
?>
<tr>
<td height="25" align="center" class="m_td">
<input type=checkbox name="chk[]" idv [ M T s ^ ,="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 $ro e % 2 n iws[5 * N J'pr! z ` % 3 w K &ice'];?>_ ] f y</ e : Otd>
&? ) clt;td height="25" align="center" class="m_td">&Q T `lt;?php echo $rows['f_time'];?></td>
<td height="25" align="center" class="m_td"><?php echo $rows['type'];?>+ V 5 F;</td>
<td class="N L 2m_td">&l} ; Y Wt;a href="#" rel="external nofollow" onClick="dea 0 e r g yl(<?php echo $rows['id'];?>)">删除</a>X S t g * $ ) r;</td>
</tr>
<?php
}
?>
<tr>
<td heightq 8 k [ . D . : L="2l 8 @ & . _ t % ;5" colspan="7" class="m_td" align="left">&nbsp;&nbsp;</td>l 0 2 - 0 m 0
</tr>
</form>
</table>
<!--show-->
&E C Ylt;table width="798" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="q Q P z : 5 * t d48" background="images/bottom.jpg">&nb0 B Ksp;</td>
</tr>
</table>
</center>
</body>
</html>

二 运行结果

ajax php如何实现表格删除数据

以上就是ajax pM x p ! 9hp如何实现表格删除数据的详细内容。