Unchecked checkbox returning null value

后端 未结 5 1101
生来不讨喜
生来不讨喜 2020-12-09 05:23

I have a set of checkboxes which when checked they pass the value as 1, otherwise they pass the value as 0. However, instead of sending the value of the unchecked checkboxe

相关标签:
5条回答
  • 2020-12-09 05:58

    That is because if you have de-selected the checkbox, the value is null, because it has no value. Check this example from w3cschools:

    http://www.w3schools.com/html/tryit.asp?filename=tryhtml_form_checkbox

    If you have selected nothing, the value does not get submitted. But if you selected something, its value will be submitted.

    If you really want true/false value, you need to trick it.. so send the state of the checked attribute will do the trick.

    $('#id:checked').length
    
    0 讨论(0)
  • 2020-12-09 05:58

    You can try this , it may help you. Assume your form is like this:

    <tr>
      <td>Active</td>
      <td><?php echo form_checkbox('active',set_value('active',$student->active),TRUE); ?>  </td>
    </tr>
    

    Now you set your codeigniter controller like this:

    // Set up the form
    $rules = $this->student_m->rules;
    $this->form_validation->set_rules($rules);
    
    // Process the form
    if ($this->form_validation->run() == TRUE) {
       $data = $this->student_m->array_from_post(array(
               'active',
               'name'
       ));
    
       // checking is checkbox is checked or not
       ($data['active'] == TRUE)?($data['active'] = 1) : ($data['active'] = 0);
    
       $this->student_m->save($data, $id);
       redirect('admin/student');
    }
    
    0 讨论(0)
  • 2020-12-09 06:17

    With jQuery you could use the checked selector to get the state of a checkbox. When it is checked, the length contains something greater than 0, e.G.:

    $('#id:checked').length
    

    So you don't need to set the value of the checkbox element to 0...

    0 讨论(0)
  • 2020-12-09 06:18

    CHANGE YOUR SCRIPT LIKE THIS. HOPE IT HELPS.

    $(document).ready(function(){
        $('#cb1, #cb2, #cb3, #cb4').click(function(){
            $(this).val(this.checked ? 1 : 0);
        });
    });
    
    0 讨论(0)
  • 2020-12-09 06:19

    If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.

    There are two ways to fix this easily:

    1) Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn't always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won't work.

    2) Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

    <input type="hidden" name="checkbox_1" value="0">
    <input type="checkbox" name="checkbox_1" value="1">
    

    Note: If your names are in an array form (ie they have square brackets in them), this won't work, as the hidden fields will increment the array count as well.

    0 讨论(0)
提交回复
热议问题