[英]How to update a table, delete entries from the table asynchronously using jquery, ajax, php, and mysql?
好的,所以我有一個頁面可以使用php在mySQL表的基礎上構建表:
table.php
$page .='<form method="POST" action="processing.php">';
$page .= "<table> \n";
$page .= "<tr>\n";
$page .= "<th>ID</th> \n <th>First Name</th> \n <th>Last Name</th> \n <th>PhoneNumber</th> \n <th>Email</th> \n";
//Loops through each contact, displaying information in a table according to login status
$sql2="SELECT cID, firstName, lastName, phoneNum, email FROM Contact WHERE oID=".$_GET['orgID'];
$result2=mysql_query($sql2, $connection) or die($sql1);
while($row2 = mysql_fetch_object($result2))
{
$page .= "<tr>\n";
$page .= "<td>".$row2->cID."</td>\n";
$page .= "<td>".$row2->firstName."</td>\n";
$page .= "<td>".$row2->lastName."</td>\n";
$page .= "<td>".$row2->phoneNum."</td>\n";
$page .= "<td>".$row2->email."</td>\n";
//Will only display these buttons if logged in
$page .= '<td><input type="checkbox" name="checkedItem[]" value="'.$row2->cID.'"></input></td>'."\n";
$page .= "<td>".makeLink("addEditContact.php?cID=".$row2->cID, "Edit")."</td>\n";
$page .="</tr>";
}
$page .= "</table>";
//Two buttons sending to processing.php to decide what to do with selected values from there
$page .= '<input name="addToContacts" type="submit" value="Add Selected Contacts To Contact List" />'."\n" ;
$page .= '<input name="deleteContacts" type="submit" value="Delete Selected Contacts" />'."\n";
$page .= "</form>\n";
mysql_close($connection);
因此,基於復選框,我可以選擇將聯系人添加到另一個表中,或者通過首先將該表單的信息發送到processing.php頁面中來刪除該表中的聯系人,該頁面決定單擊了哪個按鈕並重定向到適當的php腳本:
processing.php:
if(!empty($_POST['checkedItem']))
{
//Because addToContacts and deleteContacts take in GET instead of POST for convinience, it needs to take all of checkItems and implode it
$var=$_POST['checkedItem'];
ksort($var);
$joinedString= implode(',',$var);
//Since there are two buttons in orgDetail, it checks for which was pushed and sends it to the correct page
if(!empty($_POST['addToContacts']))
{
header('Location: addToContacts.php?cID='.$joinedString);
}
else if($_POST['deleteContacts'])
{
header('Location: deleteContacts.php?cID='.$joinedString);
}
}
else
{
//Error for not selecting any items
$page .= makeP("You have not checked off any items. Please click ".makeLink( $currentPage, "here")." to return to previous page");
}
而且由於我現在僅對刪除聯系方式感興趣。 這是deleteContacts.php
$explodedString=explode(',',$_GET['cID']);
foreach($explodedString as $eString)
{
$sql1="DELETE FROM Contact WHERE cID='".$eString."'";
mysql_query($sql1, $connection) or die($sql1);
}
header('Location: '. $currentPage);
所以從這里開始,事情變得復雜了。 當我希望頁面同步工作時,這可以正常工作。 它圍繞PHP腳本反彈,一切都很好。 如果我想直接使用jquery從table.php中刪除怎么辦。 因此,我的意思是,它將運行mysql查詢以從實際數據庫中刪除條目,並且在執行該操作之后,將更新tables.php中的表視圖以反映該更改; 全部異步完成?
(請忽略所有sql查詢都不是轉義字符串的事實,我意識到這一點,以后必須進行修復)
提前致謝。
如果您需要不使用javascript的表格,那么請首先使用此功能
在不啟用Javascript的情況下保持表最新的唯一方法是刷新頁面並在每次加載時重新繪制表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.