![]() |
Options with boolean value 0 - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Options with boolean value 0 (/showthread.php?tid=56891) Pages:
1
2
|
Options with boolean value 0 - El Forum - 01-28-2013 [eluser]cPage[/eluser] When i send my form with a control option value 0 . The field is a tinyint in MySql for bool value 0 or 1. The post don't return the field if it have a 0 value. How to avoid this ? Options with boolean value 0 - El Forum - 01-28-2013 [eluser]CroNiX[/eluser] It depends on the particular form control. Things like checkboxes don't get transmitted unless they ARE checked, but that's just the HTML specs. You can check out the "successful control" portion of the spec here. Code: //input::post() returns bool FALSE if it doesn't exist, so convert it to an int. Not sure if that is your problem here, but with no code to go by we can only guess. Options with boolean value 0 - El Forum - 01-28-2013 [eluser]cPage[/eluser] Hi CroniX thank you for your reply. I've tried to cast the input->post. But it simply not working because the field is not even set and i don't want to use @ to avoid error message. Code: $user_right_delete = (int)$this->input->post('right_delete'); So, i've changed the DB table field for VARCHAR 3 with a default value of 'off', because the return value of a checkbox is 'on' if it checked. Code: $post = $this->input->post(); I don't know if it's a good way to manage single checkbox but it works. Options with boolean value 0 - El Forum - 01-28-2013 [eluser]CroNiX[/eluser] Not sure why it wouldn't work, I use it all of the time. if 'right_delete' doesn't exist in POST (meaning it isn't a successful control), then $this->input->post('right_delete'); will return boolean FALSE. Casting a boolean FALSE to an int would make it INT(0). Not sure if it's related, but in your code you are first checking for "right_delete", but later you are using just "delete". What error message are you getting? $this->input->post('right_delete'); shouldn't cause any error message at all because it will return booean FALSE if the key value doesn't exist in the $_POST array. Options with boolean value 0 - El Forum - 01-28-2013 [eluser]CroNiX[/eluser] In your controller, try using Code: var_dump($this->input->post('right_delete')); Options with boolean value 0 - El Forum - 01-28-2013 [eluser]cPage[/eluser] Checkbox control return value FALSE if it's not set and ON if it is checked. Here's the good tests if i want to continue to use tinyint 0 or 1 in MySql for those checkboxes. Code: $post['add'] = (isset($post['add']) && $post['add']=='on')?1:0; Options with boolean value 0 - El Forum - 01-28-2013 [eluser]CroNiX[/eluser] Or you could just have the checkboxes send a value of 1 instead of "on" and then use the code I posted to cast it to an int. If it isn't checked it won't be sent and will be FALSE and converted to a INT(0), if it is checked it will be a INT(1). That's a lot of logic code that isn't really needed, and will only grow the the number of checkboxes. Code: $post['add'] = (int)$this->input->post('add'); Then the values will be ints in the db like they should be instead of a varchar. Options with boolean value 0 - El Forum - 01-28-2013 [eluser]cPage[/eluser] Code: $post['add'] = (int)$this->input->post('add'); Showing 0 even if the checkbox is ON. It seems that we cannot cast to integer the ON value of a checkbox. Options with boolean value 0 - El Forum - 01-28-2013 [eluser]CroNiX[/eluser] Yeah, which is why I said you should change your checkboxes so they transmit the value of "1" instead of "on". Options with boolean value 0 - El Forum - 01-28-2013 [eluser]cPage[/eluser] It's working now. Thanks for your help |