jquery - Pass checkbox values into an array using ajax to php -
i needing passing checkbox values array using ajax , retrieving values in controller. following code not working. receive error: "invalid argument supplied foreach()". var_dump gives string(42) "educator_classes[]=1&educator_classes;[]=3
thanks can provide.
my html form input:
<input type="checkbox" id="educator_classes[]" name="educator_classes" class="educator_classes" value="<?php echo $class_number; ?>"/>
my jquery:
$("#send_invite").click(function() { var form_data = { opportunity_id: $('#opportunity_id').val(), educator_id: $('#educator_id').val(), educator_classes: $('#educator_classes:checked').serialize(), ajax: '1' }; $.ajax({ url: "<?php echo site_url('schedule/update_educator_class'); ?>", type: 'post', data: form_data, success: function(data) { $('#response').html(data); } }); return false; })
my controller:
function update_educator_class() { $educator_id = $this->input->post('educator_id'); $opportunity_id = $this->input->post('opportunity_id'); $educator_classes = $this->input->post('educator_classes'); foreach($educator_classes $educator_class): $this->ion_auth_model->update_educator_class($opportunity_id, $educator_class, $educator_id); endforeach; }
you have use []
name attribute , not id
, otherwise can't act array
<input type="checkbox" id="educator_classes" name="educator_classes[]" class="educator_classes" value="<?php echo $class_number; ?>"/>
and jquery code can simpler:
$("#send_invite").click(function() { var form_data = $(this).closest('form).serialize(); form_data['ajax'] = 1; $.ajax({ url: "<?php echo site_url('schedule/update_educator_class'); ?>", type: 'post', data: form_data, success: function(data) { $('#response').html(data); } }); return false; });
to debut what's passed $educator_classes
can this:
var_export($educator_classes);
Comments
Post a Comment