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.

1
  • Please var_dump($checked) and show the output. Commented Jun 23, 2014 at 13:56

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the quick response. After updating the AJAX and the if statement in my controller, it always evaluates to true.
Can you add a var_dump($checked) and show the exact output? Looks like we are missing something trivial.
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?
The result of var_dump($checked) is string(4) "true"
Thanks. It seems any string is taken as true. Even if you use == to ensure non strict mode. Here'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.
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'
};

1 Comment

I get the same result as described above using $('#Tuesday').prop('checked')

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.