Unchecked flag returning a null value

I have a set of flags that, when validated, pass the value as 1, otherwise they pass the value equal to 0. However, instead of sending the value of the unchecked flags as "0", the value sent is equal to "NULL.

I have the following JS code, which should set the value to 0/1 respectively, however still the value is sent as NULL. Is there anything that can be done to make sure that the value passed in the case of an unchecked flag is 0?

$('#cb1, #cb2, #cb3, #cb4').on('click', function () $(this).val(this.checked ? 1 : 0); }); 

UPDATED Here is my html:

 <input type="checkbox" name="cb1" id="cb1" value="1" checked /> <input type="checkbox" name="cb2" id="cb2" value="1" checked /> <input type="checkbox" name="cb3" id="cb3" value="1" checked /> <input type="checkbox" name="cb4" id="cb4" value="1" checked /> 

All of them are marked by default.

If one of them is not installed, MySQL reports an error: 0SQLSTATE [23000]: Violation of integrity constraint: 1048 Column "ColumnName" cannot be zero.

This is because the columns are set to non-Null. And although I set the default value to 0, as soon as I press the submit button, I still get the error. I tried to remove the Not NULL property, but instead of pre-populating the value as 0, the database was NULL.

+7
source share
5 answers

If the box is unchecked, it is not sent, so its value is 0, if it is not checked, it will not help - it will always return NULL.

There are two ways to fix this easily:

1) Suppose that NULL in PHP parameters means that the checkbox is not selected. If the checkbox does not always exist on the page, this can be problematic. There is a variable number of flags for the sounds of this, so this probably won't work.

2) Add hidden input with the same name as the checkbox with the value 0 BEFORE . If the box is unchecked, the value of the hidden field will be used; if it is set, the value of the box 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 the form of an array (i.e. they have square brackets), this will not work, since hidden fields will increase the number of arrays.

+42
source

CHANGE YOUR SCRIPT AS IT. HOPES TO HELP.

 $(document).ready(function(){ $('#cb1, #cb2, #cb3, #cb4').click(function(){ $(this).val(this.checked ? 1 : 0); }); }); 
+1
source

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

 $('#id:checked').length 

Therefore, you do not need to set the value of the checkbox element to 0 ...

0
source

This is because if you uncheck this box, the value is null, because it does not matter. Check out this example on w3cschools:

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

If you have not selected anything, the value will not be sent. But if you select something, its value will be sent.

If you really need a true / false value, you need to trick it, so submit the status of the verified attribute by doing the trick.

 $('#id:checked').length 
0
source

You can try it, it can help you. Suppose your form is as follows:

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

Now you install your codeigniter controller as follows:

 // 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
source

All Articles