Thursday, 15 March 2012

How to update a table, delete entries from the table asynchronously using jquery, ajax, php, and mysql? -



How to update a table, delete entries from the table asynchronously using jquery, ajax, php, and mysql? -

okay, have page builds table base of operations on mysql table using php:

tables.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 info in table according login status $sql2="select cid, firstname, lastname, phonenum, email contact 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 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 processing.php decide selected values there $page .= '<input name="addtocontacts" type="submit" value="add selected contacts contact list" />'."\n" ; $page .= '<input name="deletecontacts" type="submit" value="delete selected contacts" />'."\n"; $page .= "</form>\n"; mysql_close($connection);

so base of operations on checkboxes, can take add together contacts table, or delete contacts off of table first sending info of form processing.php page decides button click , redirects proper php script:

processing.php:

if(!empty($_post['checkeditem'])) { //because addtocontacts , deletecontacts take in instead of post convinience, needs take of checkitems , implode $var=$_post['checkeditem']; ksort($var); $joinedstring= implode(',',$var); //since there 2 buttons in orgdetail, checks pushed , sends right page if(!empty($_post['addtocontacts'])) { header('location: addtocontacts.php?cid='.$joinedstring); } else if($_post['deletecontacts']) { header('location: deletecontacts.php?cid='.$joinedstring); } } else { //error not selecting items $page .= makep("you have not checked off items. please click ".makelink( $currentpage, "here")." homecoming previous page"); }

and since interested in delete contact case right now. here deletecontacts.php

$explodedstring=explode(',',$_get['cid']); foreach($explodedstring $estring) { $sql1="delete contact cid='".$estring."'"; mysql_query($sql1, $connection) or die($sql1); } header('location: '. $currentpage);

so here gets complicated. works fine when want page work synchronously. bounces around php scripts , well. if want delete straight tables.php using jquery. mean that, run mysql query delete entries actual db, , after update table view in tables.php reflect change; done asynchronously?

(please ignore fact sql queries aren't escaped strings, realize , i'll have prepare later)

thanks in advance.

if need back upwards table without javascript working first

your way maintain table date without javascript enabled refresh page , redraw table on each load.

php jquery mysql asynchronously

No comments:

Post a Comment