Hye,
Been trying to do this but still with no solution. I want to store an Input checkbox with array from a form and post the value of the selected checkbox using jQuery for delete purpose. I'm unable to get the checkbox value when user make the selection more than 1. May I know the right way to store an array value using jQuery. Below are the codes for html checkbox:
<input type="checkbox" name="checkBox[]" id="checkBox" value='".$row['staff_id']."'>
and this are the jQuery:
$(document).ready(function() {
$("#del").click(function(){
var del = $("#del").val();
var checkBox = $("#checkBox:checked").val();
$.post('admin_cuti/staff-delete.php',
{
del : del,
checkBox : checkBox
}, function(data){
$("#result_del").html(data)
});
});
});
update with full codes on my
delete.php. User can click on multiple checkboxes to delete the data. On my case, it can only delete 1 row. Below are the codes:
<?php
require_once('../db_connection/connection-intra.php');
//Delete function
if(isset($_POST['del'])){
if(empty($_POST['checkBox'])){
echo "<span style="."color:red"."><br> No record selected</span><p/>";
}
elseif(isset($_POST['checkBox'])){
$checkbox = $_POST['checkBox'];
for($i=0;$i<count($_POST['checkBox']);$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM staff WHERE staff_id='$del_id'";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
}
}
} //end of delete function
?>
Im still in the process of learning for web programming especially in jquery and javascript. Do give me an advice/comment, thanks!