CodeIgniter Forums

Full Version: Not getting the value from the on/off switch checkbox
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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); ?>
// other fields here...

<div class="form-group">
                <label for="visible" class="col-sm-2">Visible</label>
                <div class="col-sm-10">
                    <div class="onoffswitch">
                        &lt;?php  
                            $is_checked = ($post->visible) ? $post->visible : $this->input->post('visible');
                            $visible = array(
                                'class' => 'onoffswitch-checkbox',
                                'id' => 'visible',
                                'checked' => ($is_checked == '1') ? true : false,
                                'name' => 'visible',
                                'value' => $is_checked,
                                );
                        ?&gt;
                        &lt;?php echo form_checkbox($visible); ?&gt;
                        <label class="onoffswitch-label" for="visible">
                            <div class="onoffswitch-inner"></div>
                            <div class="onoffswitch-switch"></div>
                        </label>
                    </div>
                </div>
            </div>


// more fields here ...
&lt;?php echo form_close(); ?&gt;
Basicly this works as an on/off switch button. The problem is that when I click the submit button of the form, I cannot get the $_POST data of the field ( $this->input->post('visible') ) in order to pass it to my model and store it in the database.

Any ideas what I'm doing wrong and how should fix it?

El Forum

[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

El Forum

[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( );

// change 1 to your 'on' value
// change 0 to your 'off' value
$data[ 'mycheckbox' ] = ( array_key_exists( 'mycheckbox', $data ) ) ? 1 : 0;

El Forum

[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.

El Forum

[eluser]ivantcholakov[/eluser]
Yes, my comment was about 'value' => $is_checked

El Forum

[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

El Forum

[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.

El Forum

[eluser]InsiteFX[/eluser]
$checked = (isset($this->input->post('checkbox_name'))) ? TRUE: FALSE;