mysql - AJAX for successful php :: delete checked table list :: using hyperlink NOT input -
mysql - AJAX for successful php :: delete checked table list :: using hyperlink NOT input -
project focus: delete multi-checked table list form. specs: 1.) delete actioned using <a href>
hyperlink (not <input type="submit"
2.) i'd action ajax, including confirm & error/success responses. status of delete action: i've got code working delete multi-checkboxes. see successful php code snippet below. note: successful $_post coding beingness handled in same page using <input type="submit" name="delete>"
.
i've tried work, no luck. please have through coding & script see if can spot errors?
my thoughts (but uncertain): 1) ajax var formdata
written wrong accomplish getting both $delete = $_post['delete'];
, $chkbx = $_post['chkbx'];
2) instead of .click
<a href"#" id="#btn_del"
should maybe seek using .post
<form action="<?php echo $_server['php_self'];?>" method="post" name="recordsform" id="recordsform">
button updated (for spec#1) updated href
href="deleterecord.php"
<li class="button" id="toolbar-del"> <a href="#" title="delete" id="btn_del"> <span class="icon-16-delete dead"></span> delete </a> </li>
php code snippet: this code included @ bottom of form. later, move function separate actions.php page include additional button actions (edit, duplicate, archive, etc). now, i'll happy move deleterecord.php page & phone call ajax.
<? // check if delete button active, start $delete = $_post['delete']; $chkbx = $_post['chkbx']; if($delete){ for($i=0;$i<$count;$i++){ $del_id = $chkbx[$i]; $sql = "delete ".id_table." unit_id='".$del_id."'"; $result = mysqli_query($dbc,$sql); } // if successful redirect delete_multiple.php if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;url=records_manager.php\">"; }else{ echo "error: no luck"; } } mysqli_close($dbc); ?>
ajaxdelete // ajaxdelete.js $(document).ready(function() { // when trash button clicked... $('#btn_del').click(function(event) { e.preventdefault(); // stop form submitting normal way // , refreshing page // form info // there many ways info using jquery // ---------------------------------- // (you can utilize class or id also) var formdata = { 'chkbx' : $('input[name=chkbx]').val(), 'count' : $count[0] // process form // ================ $.ajax({ type : 'post', // define type of http verb want utilize url : 'deleterecord.php', // url want post info : formdata, // our info object datatype : 'json', // type of info expect server encode : true }) // using .done(), // promise callback .done(function(data) { window.console.log(data); // log info console can see // handle errors if ( ! data.success) { if (data.errors.chkbx) { $('.records_found').addclass('has-error'); $('.records_found').append('<div class="help-block">'+ data.errors.chkbx + '</div>'); } } // end if errors else { $('.records_found').append('<div class="alert alert-success" id="valid_success">'+ data.message + '</div>'); // after form submission, // redirect user page window.location = 'records_manager.php'; } }) .fail(function(data) { // promise callback window.console.log(data); }); // show errors in console // note: it's best remove production event.preventdefault(); // stop form submitting normal way // , refreshing page }); // end submit button }); // end document ready
deleterecord.php <?php // function delete // =========================== // :checked existing unit info $errors = array(); // array hold validation errors $data = array(); // array pass info if ( empty($_post['chkbx'])) // if empty, populate error $errors['chkbx'] = 'no items have been checked yet.'; // error! homecoming response if ( ! empty($errors)) { $data['success'] = false; // errors = homecoming success boolean of false $data['errors'] = $errors; // homecoming errors } else { // no error... carry on // process form info require_once('config.php'); // connect database $dbc = mysqli_connect(db_host, db_user, db_password, db_name) or die ('error connecting mysql server.'.$dbc); // check if delete $delete = $_post['delete']; $chkbx = $_post['chkbx']; $count = $_post['count']; if($delete){ for($i=0;$i<$count;$i++){ $del_id = $chkbx[$i]; $sql = "delete ".id_table." unit_id='".$del_id."'"; $result = mysqli_query($dbc,$sql); } // if successful redirect if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;url=records_manager.php\">"; }else{ echo "error: no luck"; } } mysqli_close($dbc); // close db connection } $data['success'] = true; // show message of success $data['message'] = 'success!'; // , provide true success variable } echo json_encode($data); // homecoming our info ajax phone call } // end else no errors, process form ?>
digging through loads of bookmarks, i'd found illustration of hoping achieve. after fiddling snippets of codes, i've gotten ajax work i'd hoped accomplish step of project. assist others in search this, below, i've provided coding both ajax/jq/js & php worked flawlessly in tests.
how code works the button (a hyperlinkoutside of form
, not input button
) linked deletedrecord.php
, script overrides link e.preventdefault()
jq used build array of checked row ids & send them deletedrecord.php
via ajax deleterecord.php
explodes array, counts total number of checked ids, , loops through query delete each. upon successful completion, response of 1
echo'd trigger success action hope helps out there. if sees other faults might've missed, please sense free share greater good. cheers.
ajaxdelete.jsnotes: 1.) updated image (button) href
href="deleterecord.php"
2.) researched & found improve approach reduced count checked (i thought more effective (faster) in case table grew big number of rows.
$(document).ready(function() { $('#btn_del').click(function(e) { e.preventdefault(); page = $(this).attr("href"); ids = new array() = 0; $(".chk:checked").each(function(){ ids[a] = $(this).val(); a++; }) // alert(ids); if (confirm("are sure want delete these courses?")) { $.ajax({ url : page, type : "post", info : "id="+ids, datatype : 'json', success : function(res) { if ( res == 1 ) { $(".chk:checked").each(function() { $(this).parent().parent().remove(); }) // end if remove table row } // end if res ==1 } // end success response function }) // end ajax } // end confirmed homecoming false; }); // end button click function }); // end doc ready
deleterecord.php <?php require_once('config.php'); // connect database $dbc = mysqli_connect(db_host, db_user, db_password, db_name) or die ('error connecting mysql server.'.$dbc); $del_id = explode(",",$_post['id']); $count = count($del_id); if (count($count) > 0) { foreach ($del_id $id) { $sql = "delete ".id_table." unit_id='" . $id . "'"; $result = mysqli_query($dbc,$sql) or die(mysqli_error($dbc)); } // end each mysqli_close($dbc); // close mysql echo json_encode(1); // homecoming json res == 1 // used success action } // end if count > 0 ?>
php mysql ajax delete-row sql-delete
Comments
Post a Comment