0

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.

Cludas18
  • 65
  • 3
  • 12
  • The problem is not clear to me. Please show the PHP code you've tried in order to echo the value of each checkbox and let us know what goes wrong. – showdev Jul 13 '15 at 17:16
  • There's very little PHP code but I just edited the post. – Cludas18 Jul 13 '15 at 17:22
  • I believe that's the nature of jQuery's [serialize()](https://api.jquery.com/serialize/). It encodes a set of form elements as a string. So, the `SelectedTemplates` value posted from your AJAX code is a string representation of all of the posted checkboxes. This post might be informative: [How do I PHP-unserialize a jQuery-serialized form?](http://stackoverflow.com/questions/1792603/how-do-i-php-unserialize-a-jquery-serialized-form) – showdev Jul 13 '15 at 17:25
  • %5B is [ and %5D is ] so it says: `RelatedTemplates[] = 2` since the name of all checkboxes is the same this seems right. – TheFrozenOne Jul 13 '15 at 17:27
  • Ah, ok. I wasn't aware that's how the serialize method worked, it's on eof the few times I've used it. So is there another way to go about doing what I'm trying in PHP, just to get the value of the checkbox? Or should I just send the values themselves through AJAX? – Cludas18 Jul 13 '15 at 17:31
  • 1
    I think the way you're doing it is fine. Have you tried using PHP's [`parse_str()`](http://php.net/manual/en/function.parse-str.php) on the `SelectedTemplates` variable as suggested in [this answer](http://stackoverflow.com/questions/1792603/how-do-i-php-unserialize-a-jquery-serialized-form#answer-3817220)? That will give you an array of all the checked values. Here's a [working example](http://sandbox.onlinephpfunctions.com/code/91f2fe2279414e3e292eda1506606da5927baabe). – showdev Jul 13 '15 at 17:46
  • I was a bit confused about how the `parse_str()` function worked but I just figured it out and everything is working now. Thank you! – Cludas18 Jul 13 '15 at 18:27
  • Note to reviewers: Also possible duplicate of [Parse a jquery serialize string in PHP](http://stackoverflow.com/questions/11247596/parse-a-jquery-serialize-string-in-php) or [jQuery to PHP - serialized strings](http://stackoverflow.com/questions/8318032/jquery-to-php-serialized-strings) – showdev Jul 13 '15 at 18:50

0 Answers0