I have 10 check boxes on a page. I have a "check all" button, works perfect.
I have 4 submit buttons - 2 to delete the records, and 2 to suspend the records.
The checkboxes are not contained by a form.
<button type="button" class="btn btn-small btn-warning tip suspend" data-original-title="Suspend"><span class="icon-minus icon-white"></span></button>
<button type="button" class="btn btn-small btn-danger tip delete" data-original-title="Delete"><span class="icon-remove icon-white"></span></button>
<input type="checkbox" class="checkbox" value="0" />
<input type="checkbox" class="checkbox" value="1" />
<input type="checkbox" class="checkbox" value="2" />
<input type="checkbox" class="checkbox" value="3" />
<input type="checkbox" class="checkbox" value="4" />
<input type="checkbox" class="checkbox" value="5" />
<input type="checkbox" class="checkbox" value="6" />
<input type="checkbox" class="checkbox" value="7" />
<input type="checkbox" class="checkbox" value="8" />
<input type="checkbox" class="checkbox" value="9" />
<button type="button" class="btn btn-small btn-warning tip suspend"
data-original-title="Suspend"><span class="icon-minus icon-white"></span>
</button>
<button type="button" class="btn btn-small btn-danger tip delete"
data-original-title="Delete"><span class="icon-remove icon-white"></span>
</button>
When a delete button is clicked, I need to send all checked boxes via ajax to a delete_record.php
When a suspend button is clicked, I need to send all checked boxes via ajax to a suspend_record.php
I know I need to do something like
$("button.delete").on('click', function(){
$("input:checkbox[class=checkbox]").each(function () {
});
});
But honestly at this point I am just trial and error testing code I find online. I'd rather have a clear cut answer and explanation.
Equally how would I spit this out and have php (PDO db connections) process it as an array?
Took my hours to get this but here we are
the jquery/ajax:
$(document).ready(function(){
$('.delete').click(function(){
var checkValues = $('input[type=checkbox]:checked').map(function(){
return $(this).val();
}).get();
$.ajax({
url: 'delete_records.php',
type: 'post',
data: {ids:checkValues},
success:function(data){
}
});
});
});
The first function in the jquery grabs all the values from checkbox and sets them as a variable array - checkValues.
The second part secnds the values as an array to the php page.
And the php:
$data = $_POST['ids'];
foreach ($data as $d) {
$query = $db->prepare("DELETE FROM sites WHERE username=:username AND id=:id");
$query->bindValue(':username', $username);
$query->bindValue(':id', $d);
$query->execute();
}
Sets the array to a variable $data and then runs each result as a foreach loop.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments