0

I'm having an issue with checking the value of a checkbox in CodeIgniter. I am trying to implement the solution found here: Codeigniter checking checkbox value. The following code never never evaluates to true, regardless of the checkbox being checked or not. If I add value="1" to the input, it always evaluates true.

My view:

....
<input name="Tuesday" id="Tuesday" type="checkbox" />
....

<script type="text/javascript">
.on('finished', function(e) {
    var form_data = {
        Tuesday: $('#Tuesday').val(),
        ajax: '1'
    };

    $.ajax({
        url: "<?php echo site_url('opportunity/create_opportunity'); ?>",
        type: 'POST',
        data: form_data,
        dataType: 'json',
        cache: false
    });
})
</script>

My controller:

function create_opportunity() {

     if($this->input->post('ajax')) {
        $checked = $this->input->post('Tuesday');

        if((int) $checked == 1) {
            $Tuesday = array(
                'Week_Day_Id' => 2,
                'Opportunity_Id' => 18,
                'Preferred' => $checked
            );
        }

        $this->ion_auth_model->create_opportunity($Opportunity, $Tuesday);
    }
}

Thanks for any help you can provide.

Community
  • 1
  • 1
Derek Morgan
  • 165
  • 1
  • 2
  • 16

2 Answers2

0

In your AJAX, you are using $('#Tuesday').val() which will look for value attribute in your HTML. Since value attribute is empty, it might be sending empty.

The solution is to use $('#Tuesday').is(':checked') which will return a Boolean.

In PHP just use if ($checked == TRUE)

  • Thanks for the quick response. After updating the AJAX and the if statement in my controller, it always evaluates to true. – Derek Morgan Jun 23 '14 at 14:07
  • Can you add a `var_dump($checked)` and show the exact output? Looks like we are missing something trivial. –  Jun 23 '14 at 14:11
  • For some reason, var_dump() is returning null in my log file. However, I am able to get this to work `if ($checked == 'true')`. Looks like my variable type is string? – Derek Morgan Jun 23 '14 at 16:19
  • The result of var_dump($checked) is string(4) "true" – Derek Morgan Jun 23 '14 at 18:40
  • Thanks. It seems any string is taken as true. Even if you use == to ensure non strict mode. [Here](http://stackoverflow.com/questions/7336861/how-to-convert-string-to-boolean-php)'s a detailed explanation and solution to your problem. To be honest, I did not know about this. Though I have dealt with hundreds of forms like these, wonder why I never encountered it. –  Jun 24 '14 at 03:56
0

It looks like your only checking the value, I would actually check if it's checked or not using jquery and send that value instead. So instead of this:

.on('finished', function(e) {
var form_data = {
    Tuesday: $('#Tuesday').val(),
    ajax: '1'
};

I would do something like this:

.on('finished', function(e) {
var form_data = {
    Tuesday: $('#Tuesday').prop('checked') // this should send a true or false value
    ajax: '1'
};
jamadri
  • 926
  • 3
  • 17
  • 32