Sunday, 15 July 2012

php - Submitting checkboxes with jquery and ajax -



php - Submitting checkboxes with jquery and ajax -

so have form table in php page:

$page .='<form method="post" action="delete.php" id="orgform">'; $page .= "<table> \n"; $page .= "<tr>\n"; //decides display based on logged in. if logged in can see contact's info $page .= "<th>id</th> \n <th>first name</th> \n <th>last name</th> \n <th>phone number</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"; $page .= '<td><input type="checkbox" name="checkeditem[]" value="'.$row2->cid.'"></input></td>'."\n"; $page .="</tr>"; } $page .= '<input name="deletecontacts" type="submit" value="delete selected contacts" />'."\n"; $page .= "</form>\n"; $page .='<script src="assets/js/orgdetails.js" type="text/javascript"></script>'."\n";

i need somehow write jquery script within orgdetails.js able delete checked rows when force delete button. alter have appear on screen without refresh, , need able delete actual row sql db well. can please give me hand? thanks.

in action url delete.php, after submit post:

if ($_post != array()) { foreach ($_post['checkeditem'] $id) { mysql_query('delete contact cid='.$id); } echo 'records deleted.'; }

if dont want page refresh when delete records:

add html:

<button class="delete_button">delete selected records</button>

add js file:

$('.delete_button').click(function () { $form = $('#orgform'); delete_ids = []; $form.find('input[name=checkeditem]').each(function () { $checkbox = $(this); if ($checkbox.is(':checked')) { delete_ids.push($checkbox.val()); } ); $.ajax({ url: 'delete.php', type: 'post', data: {delete_ids: delete_ids}, success: function (result_html) { alert(result_html); }, }); });

and in delete.php:

if ($_post != array()) { foreach ($_post['delete_ids'] $id) { mysql_query('delete contact cid='.$id); } echo 'records deleted.'; }

php jquery ajax forms checkbox

No comments:

Post a Comment