0

I am needing some help passing checkbox values into an array using ajax and retrieving the values in my controller. The following code is not working. I receive the error: "Invalid argument supplied for foreach()". Var_dump gives string(42) "Educator_Classes[]=1&Educator_Classes;[]=3

Thanks for any help you 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 as $Educator_Class):
        $this->ion_auth_model->update_educator_class($Opportunity_Id, $Educator_Class, $Educator_Id);
    endforeach;
}
7
  • 1
    Select educator_classes like this Educator_Classes: $('[id="Educator_Classes[]"]').serialize()or Educator_Classes: $('#Educator_Classes\\[\\]').serialize() Commented Sep 10, 2014 at 13:35
  • @Anton Hmm, tried both and still getting the same result. Thanks for the quick reply. Commented Sep 10, 2014 at 13:39
  • Ah i noticed it's the classes you want. You need to use class indicator . $('.Educator_Classes:checked') Commented Sep 10, 2014 at 13:40
  • @Anton Still same error w/ Educator_Classes: $('.Educator_Classes\\[\\]').serialize(). Commented Sep 10, 2014 at 13:53
  • 1
    without [] it should be $('.Educator_Classes:checked').serialize() Commented Sep 10, 2014 at 13:56

2 Answers 2

2

You have to use [] for name attribute and not for id, otherwise it can't act like an array

<input type="checkbox" id="Educator_Classes" name="Educator_Classes[]" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>

And also your jQuery code can be 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 to $Educator_Classes you can do this:

var_export($Educator_Classes);
Sign up to request clarification or add additional context in comments.

2 Comments

var_dump gives string(24) "Educator_Classes[]=1&Educator_Classes;[]=3
Serializing the entire form solved the problem! Thanks
2

The Solution is that you have to take the array of name attribute of checkbox. Like :

<input type="checkbox" id="Educator_Classes[]" name="Educator_Classes[]" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>

2 Comments

Use this<input type="checkbox" id="Educator_Classes" name="Educator_Classes[]" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>
That is what I am using, with the same result...<input type="checkbox" id="Educator_Classes" name="Educator_Classes[]" class="Educator_Classes" value="<?php echo $Class_Number; ?>"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.