Unable to write checkbox to mysql db |
Hi All,
How do I read the checkbox value (0 or 1) from a submitted form, and then update a db please? I have a form which updates every record in the db except the checkbox. If I use (as part of an array) - 'SwimmerLeft' => $_POST['SwimmerLeft'], - I get an error $_POST['SwimmerLeft'] is undefined. Yours Roy Bee Creative Web Design www.bcwd.ltd.uk
Hi,
You collect the value of a checkbox just like any other input. Except that with a checkbox if it is not checked the value is not submitted. So if you are not getting the value you should check the name of the checkbox is correct, and that it is checked. Hope that helps, Paul.
It's correct as I am reading the field to populate the form, but it won't write it whether it has changed or not.
Roy
If it is not checked it will be nothing in that case you need to supply the 0 (zero) to save it to database like the one above.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Please write,
$this->checkbox=0; if($this->input->post('checkbox')) { $this->checkbox=1; }
I know what the problem is now, but I still can't find the answer -
This code returns 'null' whether I check the box or not, so when I run the if, I always get '0' Code: echo '<tr><td class="app">'.form_label('Swimmer Left:').'</td><td class="app">'.form_input('LeavingDate', $dateLeft).'</td><td class="app">'.form_checkbox('SwimmerLeft','', $row->SwimmerLeft).'</td></tr>'; So have I formatted the checkbox wrong? The form displays the checkbox correctly and if I untick it, it set the field to '0', but if I tick the box nothing changes. Yours Roy Bee Creative Web Design www.bcwd.ltd.uk (12-01-2015, 02:21 AM)GingerNut Wrote: I know what the problem is now, but I still can't find the answer - Your should specify value for checkbox that will return when submitting the form PHP Code: form_checkbox('SwimmerLeft','1', $row->SwimmerLeft); Read more about form_checkbox()
Thanx everyone..................sorted now with your help
Yours Roy Bee Creative Web Design www.bcwd.ltd.uk |
Welcome Guest, Not a member yet? Register Sign In |