I currently update my database records with an input like this
$this->my_model->update($id, $this->input->post()));
(This is after I have performed a validation on all the input. The “model” also has a white_list of data to expect.)
Problem: if I am updating a checkbox, and it is “unticked” (i.e. ‘false’) – then that field is not ‘posted’ by the browser.
For example – if I tick checkbox_two – but leave checkbox_one unticked, $_POST shows:
[field_one] = "some value"
[field_two] = "some value"
[checkbox_two] = 1
Therefore my model will not update that field – since it is not part of the post.
The same does not happen in reverse – because a “ticked” checkbox is posted as “1” – and thus is correctly updated.
[field_one] = "some value"
[field_two] = "some value"
[checkbox_one] = 1
[checkbox_two] = 1
Question: Does anyone have an elegant solution to handle this, other than having to always specifically check for each checkbox?
This practice is easy and logical. If checkbox is checked,
$this->input->post('example') == 'TRUE'else,$this->input->post('example') == 'FALSE'.The latest given value of a
name=""overrides previous, thus givin priocheckbox>hidden.EDIT: this solution is equal to given most rated answer in Rick Calder’s comment. I’ve also come to this conclusion on my own, though.