Welcome Guest, Not a member yet? Register   Sign In
form_checkbox doesn't check the box when updating the DB
#1

Not sure how to explain this:
I use the form helper for most html input functions. I pull from the database and pre-populate the form to update information.

When I use the form_checkbox, if the option is set (0 for FALSE), when I check the box and click on update, it will not show the check in the box even though it updated the database on the next refresh.

I managed to get it working but not using form_checkbox (in this instance) helper function.

Here is the code:
<div class="form-group form-group-lg row"> 
                                    <?php $extra = array('class' => 'col-4 col-form-label col-form-label-md');
                                    echo form_label('Extra / Bonus Ball?''extra_ball_lb'$extra); ?>
                                    <div class="col-8" style="margin-top:10px;">
                                        <input type = "checkbox" name = "extra_ball" value = "1" <?php echo (!empty($lottery->extra_ball) ? 'checked' : ''); ?> /> 
                                        <?php //echo form_checkbox('extra_ball', '1', set_checkbox('extra_ball', '1', (!empty($lottery->extra_ball)))); ?>
                                    </div>
                                </div>

I commented out the form_checkbox since it does.

What's really strange, (on the same page), I have very similar code that works perfectly but I can't explain why this one works and the code above doesn't work? 
Here is a code that works on the same page:
<div class="form-group"> 
                            <?php $extra = array('class' => 'col-4 col-form-label col-form-label-sm');
                            echo form_label('Monday''day_monday_lb'$extra); 
                            echo form_checkbox('monday'set_value('monday''1'), set_checkbox('monday''1', (!empty($lottery->monday)))); 
                            echo form_label('Tuesday''day_tuesday_lb'$extra); 
                            echo form_checkbox('tuesday''1'set_checkbox('tuesday''1', (!empty($lottery->tuesday))));
                            echo form_label('Wednesday''day_wednesday_lb'$extra); 
                            echo form_checkbox('wednesday''1'set_checkbox('wednesday''1', (!empty($lottery->wednesday))));
                            echo form_label('Thursday''day_thursday_lb'$extra); 
                            echo form_checkbox('thursday''1'set_checkbox('thursday''1', (!empty($lottery->thursday))));
                            echo form_label('Friday''day_friday_lb'$extra); 
                            echo form_checkbox('friday''1'set_checkbox('friday''1', (!empty($lottery->friday))));
                            echo form_label('Saturday''day_saturday_lb'$extra); 
                            echo form_checkbox('saturday''1'set_checkbox('saturday''1', (!empty($lottery->saturday))));
                            echo form_label('Sunday''day_sunday_lb'$extra); 
                            echo form_checkbox('sunday''1'set_checkbox('sunday''1', (!empty($lottery->sunday)))); 
                            echo form_error('monday''<div class="bg-warning" style = "margin-top:10px; padding: 10px; text-align: center; color:#ffffff; font-size:16px;">''</div>'); ?>
                        </div>
Reply
#2

Hello,

Simple questions...
- What do you get when you print_r/var_dump ($lottery->extra_ball) after the database has been updated ? 1 ? You say the record has been saved successfully in database, but is it sent back to the view correctly ?
- Did you try not using special chars in your label "Extra / Bonus Ball?" ? Because when I look at your code, this is the only difference I see. Might be interesting to try, even though the probability is very very low, you have to check everything.
Reply
#3

Also worth checking the return type of the checkbox, depending where the control originated (probably a Bootstrap control). I've had the following values returned from checkboxes (which are often a pain): value could be "on", "checked" etc. whne using Bootsrap. Checkboxes do NOT use 0 as a false value.

Remember with checkboxes, if the value is not set, the return is NOT SET (unassigned - the property does not exist). If it is set (checked) then the value will be set (but could be "on" "checked" etc. Make sure also the value is NOT assigned to the value attribute (... value=1) instead have it as the last param in the list (... name="bob" style="blah" checked> ).
Reply




Theme © iAndrew 2016 - Forum software by © MyBB