How to re-populate check box in edit form using Codeigniter [duplicate]

风流意气都作罢 提交于 2019-12-25 18:29:13

问题


So far I know how to repopulate the form input and checkbox

It looks like that:

value="<?= set_value('rank') ?>" for input 

and

 <?= set_checkbox('is_default', '1'); ?> for checkbox

The problem is, how about in the edit form:

The input value is sofar ok:

value="<?= set_value('rank',$customer_group[0]['rank']); ?>"

but I can't repopulate the checkbox

<?php if ($customer_group[0]['is_default'] == "1") echo "checked"; set_checkbox('is_default', '1'); ?>

The checkbox will check even I have not check it in the edit => fail to validate in the form, thanks for helping

Update:

Is it correct if change to :

<?php
    if(isset($_POST['is_default']) || $customer_group[0]['is_default'] == "1"){
    echo "checked"; 
    }
    set_checkbox('is_default', '1'); 
 ?>

回答1:


A simple one just check for post data too if validation fails

<?php
    if(isset($_POST['is_default'])){
    echo "checked"; set_checkbox('is_default', '1');
    }elseif ($customer_group[0]['is_default'] == "1"){
    echo "checked"; set_checkbox('is_default', '1');
    }
 ?>

So if user checks checkbox and validation fails the form will have a checked check box as to save the choice made by user if there is no post data then it will be checked by database data that is in $customer_group



来源:https://stackoverflow.com/questions/25351381/how-to-re-populate-check-box-in-edit-form-using-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!