I've spent quite a bit of time looking up how I could do this. Basically, I have a few checkboxes that I am sending through AJAX to a PHP script, and I want the PHP script to echo the value of each selected checkbox.
Here is the code for my checkboxes:
while ($template = $stmt->fetch(PDO::FETCH_ASSOC)){
echo("<input class='checkbox templateSelectBox' name='RelatedTemplates[]' data-template='". $template['ID'] ."' value='".$template['ID']."' type='checkbox'> <p class='label label-default'>". $template['Name'] ."</p><br>");
}
With that PHP code, there are 3 checkboxes on the page. Here is the AJAX that sends the checked checkboxes:
$.ajax({
type: 'POST',
url: 'inc/ajax/addserver.ajax.php',
data: {'SelectedTemplates': $(".templateSelectBox:checked").serialize()},
success: function(data){
$("#responses").html(data);
} })
Then say if I check the second checkbox and echo it using echo($_POST['SelectedTemplates'])
, it echoes RelatedTemplates%5B%5D=2
, which I don't fully understand why. All I'm expecting is the value of the checkbox, which is only a single digit. Instead I'm getting that string.