CodeIgniter检查复选框的值

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.

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)

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'
};