I want to delete multiple rows from database by checkboxes i have working script for "Check All" but when i want delete one or two , nothing happend.
JavaScript
<script type="text/javascript">
jQuery(function($) {
$("form input[id='check_all']").click(function() { // triggred check
var inputs = $("form input[type='checkbox']"); // get the checkbox
for(var i = 0; i < inputs.length; i++) { // count input tag in the form
var type = inputs[i].getAttribute("type"); // get the type attribute
if(type == "checkbox") {
if(this.checked) {
inputs[i].checked = true; // checked
} else {
inputs[i].checked = false; // unchecked
}
}
}
});
$("form input[id='submit']").click(function() { // triggred submit
var count_checked = $("[name='data[]']:checked").length; // count the checked
if(count_checked == 0) {
alert("Please select a comment(s) to delete.");
return false;
}
if(count_checked == 1) {
return confirm("Are you sure you want to delete these comment?");
} else {
return confirm("Are you sure you want to delete these comments?");
}
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$('.submit').click(function(){
var checkValues = $('input[name=data[]]:checked').map(function()
{
return $(this).val();
}).get();
$.ajax({
url: 'resources/ajax/ajax_delete_comment.php',
type: 'post',
data: { data: checkValues },
success:function(data){
}
});
});
});
</script>
HTML/PHP
<form method="post" id="form">
Check All <input type="checkbox" id="check_all" value="">
Here im displaying record from database and <input name=\"data[]\" type=\"checkbox\" id=\"data\" value=" . $row['id'] . ">
<input name="submit" class="submit" type="submit" value="Delete" id="submit">
</form>
DELETING SCRIPT
if(isset($_POST['data'])) {
$id_array = $_POST['data']; // return array
$id_count = count($_POST['data']); // count array
for($i=0; $i < $id_count; $i++) {
$id = $id_array[$i];
$sql = $db->query("DELETE FROM comments WHERE `id` = '$id'");
if ($sql)
{
echo "success";
}
else
{
echo "Failed to delete the comment.";
}
}}
So its work for check all, but when im checking one or two objects, nothing happend, maybe someone could help?