0

I have a jQuery form in which I create a series of checkboxes:

<?php
<form method="post" id="b-form" action="../createb.php">
    for ($i=0; $i<$request_count; $i++){
       <div class="request-check">
          <table>
             <tr>
                <td><input type="checkbox" name="show_request[]" value="request".$i."      checked="checked"/>select request</td>
             </tr>
          </table>
        </div>
   }

javascript 
$.ajax({
          type: 'POST',
          url: '../createb.php',
          data: $('#b-form').serialize(),
          success: function (msg){
            alert(msg);
          }
        })

at the moment createb.php is just testing the form

  $requests = $_POST['show_request'];
  $request_count = count($requests);
  echo 'count: '.$request_count;
  echo $requests[0];

The Problem is that the serialize function only sees the first checkbox and indicates whether it has been checked or not. It does not see any of the other check boxes. Does anybody have an idea why the other check boxes do not get serialized and what to do about it?

Thanks
David

Edited by pritaeas: Fixed formatting

2
Contributors
2
Replies
5
Views
6 Years
Discussion Span
Last Post by Davife
0

It looks like all of your check boxes have the same name, putting them into a group. So you would only get the result of the checked box. using jQuery tho, you could loop through them and put the checked box values into one array and send that..

assuming these are the only check boxes on the page

var array_values = [];
$('input[type=checkbox]').each( function() {
    if( $(this).is(':checked') ) {
        array_values.push( $(this).val() );
    }
});
// Now you can store your values in a comma separated list
var arrayValues = array_values.join(',');

// Now just change your data: line in the Ajax
data: {
    'show_request': arrayValues
},

Hope that helps

1

Hi Jim,

The problem was actually my error in the html and has been solved. My actual code was much longer than what was shown in the problem so it wasn't immediately obvious. Having said that, I like your solution. It's clean and straight forward.

Thanks,
David

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.