您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)ajax php實(shí)現(xiàn)表格刪除數(shù)據(jù)的方法的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧。
ajax php實(shí)現(xiàn)表格刪除數(shù)據(jù)的方法:首先連接數(shù)據(jù)庫(kù);然后在PHP文件中把傳過來(lái)的參數(shù)值賦給變量“$i”,并根據(jù)參數(shù)值執(zhí)行相應(yīng)的刪除操作;接著在js文件中實(shí)現(xiàn)刪除判斷;最后循環(huán)輸出查詢結(jié)果并刪除即可。
Ajax+PHP實(shí)現(xiàn)的刪除數(shù)據(jù)功能。
一 代碼
conn.php:
<?php $conn = mysql_connect("localhost", "root", " ") or die("連接數(shù)據(jù)庫(kù)服務(wù)器失敗!".mysql_error()); //連接MySQL服務(wù)器 mysql_select_db("db_database27",$conn); //選擇數(shù)據(jù)庫(kù)db_database27 mysql_query("set names utf8"); //設(shè)置數(shù)據(jù)庫(kù)編碼格式utf8 ?>
del.php:
<?php include_once("conn/conn.php");//包含數(shù)據(jù)庫(kù)連接文件 $id=$_GET['id'];//把傳過來(lái)的參數(shù)值賦給變量$i $sql=mysql_query("delete from tb_demo02 where id=".$id);//根據(jù)參數(shù)值執(zhí)行相應(yīng)的刪除操作 if($sql){//如果操作的返回值為true $reback=1;//把變量$reback的值設(shè)為1 }else{ $reback=0;//否則變量$reback的值設(shè)為0 } echo $reback;//輸出變量$reback的值 ?>
index.js:
function del(id){ var xml; if(window.ActiveXObject){//如果是瀏覽器支持ActiveXObjext則創(chuàng)建ActiveXObject對(duì)象 xml=new ActiveXObject('Microsoft.XMLHTTP'); }else if(window.XMLHttpRequest){//如果瀏覽器支持XMLHttpRequest對(duì)象則創(chuàng)建XMLHttpRequest對(duì)象 xml=new XMLHttpRequest(); } xml.open("GET","del.php?id="+id,true);//使用GET方法調(diào)用del.php并傳遞參數(shù)的值 xml.onreadystatechange=function(){//當(dāng)服務(wù)器準(zhǔn)備就緒執(zhí)行回調(diào)函數(shù) if(xml.readystate==4 && xml.status==200){//如果服務(wù)器已經(jīng)傳回信息并未發(fā)生錯(cuò)誤 var msg=xml.responseText;//把服務(wù)器傳回的值賦給變量msg if(msg==1){//如果服務(wù)器傳回的值為1則提示刪除成功 alert("刪除成功!"); location.reload(); }else{//否則提示刪除失敗 alert("刪除失??!"); return false; } } } xml.send(null);//不發(fā)送任何數(shù)據(jù),因?yàn)閿?shù)據(jù)已經(jīng)使用請(qǐng)求URL通過GET方法發(fā)送 }
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>分組統(tǒng)計(jì)</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">價(jià)格</td> <td width="20%" class="top">出版時(shí)間</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的數(shù)據(jù) $result = mysql_query($sqlstr1,$conn);//執(zhí)行查詢語(yǔ)句 while ($rows = mysql_fetch_array($result)){//循環(huán)輸出查詢結(jié)果 ?> <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>
二 運(yùn)行結(jié)果
感謝各位的閱讀!關(guān)于ajax php實(shí)現(xiàn)表格刪除數(shù)據(jù)的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。