Welcome Guest, Not a member yet? Register   Sign In
Unable to write checkbox to mysql db
#1

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
Reply
#2

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.
Reply
#3

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
Reply
#4

May be you should do this

if($this->input->post('checkbox')){
 $this->checkbox=1;
}else{
 $this->checkbox=0;
}
Tafsir NU adalah sebuah aplikasi yang berisi kumpulan kajian kitab kuning yang disampaikan oleh Kiai Nahdhotul Ulama seperti Gus Baha, gus mus, dan kiai lainnya dalam format audio.
Reply
#5

(This post was last modified: 11-30-2015, 08:38 PM by InsiteFX.)

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 )
Reply
#6

Please write,

$this->checkbox=0;
if($this->input->post('checkbox'))
{
$this->checkbox=1;
}
Reply
#7

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
Reply
#8

(12-01-2015, 02:21 AM)GingerNut Wrote: 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

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()
Reply
#9

Thanx everyone..................sorted now with your help Smile
Yours Roy
Bee Creative Web Design
www.bcwd.ltd.uk
Reply




Theme © iAndrew 2016 - Forum software by © MyBB