![]() |
Not getting the value from the on/off switch checkbox - 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: Not getting the value from the on/off switch checkbox (/showthread.php?tid=60456) |
Not getting the value from the on/off switch checkbox - El Forum - 03-31-2014 [eluser]Lykos22[/eluser] Hi, I'd like some help please. I have this form group: This is the view: Code: <?php echo form_open('admin/posts/post/'.$post->id); ?> Any ideas what I'm doing wrong and how should fix it? Not getting the value from the on/off switch checkbox - El Forum - 03-31-2014 [eluser]ivantcholakov[/eluser] It is not necessary the value property to be set in conditional manner. I would just write 'value' => 1 When a checkbox is checked, its value property exists in $_POST under the corresponding name. On a unchecked box - the value is missing. In order always to track the state of the checkbox you may use the following Checkbox Helper: https://gist.github.com/mikedfunk/4004986 Edit: Ha, this one is better: https://gist.github.com/pporlan/8316087 Not getting the value from the on/off switch checkbox - El Forum - 03-31-2014 [eluser]jonez[/eluser] As ivantcholakov said the key will only exist if it was checked. When you save the form data to your database check if the key was submitted, if it was set it to 'on' if the key was omitted set it to 'off'. Code: $data = $this->input->post( ); Not getting the value from the on/off switch checkbox - El Forum - 03-31-2014 [eluser]Lykos22[/eluser] [quote author="ivantcholakov" date="1396257528"]It is not necessary the value property to be set in conditional manner. I would just write 'value' => 1[/quote] Are you reffering to the $is_checked ? Doesn't make sence to check the state off the checkbox? I mean It can't be always checked ('value' => 1) there maybe times where the checkbox is not checked at all, or you want to change its status. Not getting the value from the on/off switch checkbox - El Forum - 04-01-2014 [eluser]ivantcholakov[/eluser] Yes, my comment was about 'value' => $is_checked Not getting the value from the on/off switch checkbox - El Forum - 04-01-2014 [eluser]Lykos22[/eluser] I'll give a try to this snippet you've posted as soon as I get some free time and I'll inform you if I have any problems Not getting the value from the on/off switch checkbox - El Forum - 04-03-2014 [eluser]Lykos22[/eluser] I didn't try the code you posted, instead I set a default value and then I do a check if the $this->input->post('visible') is checked or not. It seems to work pretty good actually. Not getting the value from the on/off switch checkbox - El Forum - 04-04-2014 [eluser]InsiteFX[/eluser] $checked = (isset($this->input->post('checkbox_name'))) ? TRUE: FALSE; |