简体   繁体   中英

Codeigniter updating database with checkbox

i have a table named group where i have columns like admin,editor,user,guest they are all boolean.
i want to create a group using checkbox. so in my view i got checkbox like admin,editor,user,guest and a create button. When i click the create button i want the checked options to be true in database and unchecked remains false. how can i do that?

I am new in codeigniter.

<html>
<head>
    <title>Create A group</title>
</head>
<body>
    <?php echo form_open('group/create_group'); ?>
    <div>
        <?php echo form_label("Gruop Name"); ?> 
        <?php 
        $data=array(
        'name'=>'group_name',
        'placeholder'=>'enter group name'
        );
     ?>
         <?php echo form_input($data); ?>
    </div>
    <div>
        <?php $data=array(
        'name'=>'role[]',
        'value'=>'1',
        'type'=>'checkbox'
        );
        ?>
        <?php echo form_label('Admin'); ?>
        <?php echo form_checkbox($data); ?>
    </div>
    <div>
        <?php $data=array(
        'name'=>'role[]',
        'value'=>'2'
        );
        ?>
        <?php echo form_label('User'); ?>
        <?php echo form_checkbox($data); ?>
    </div>
    <div>
        <?php $data=array(
        'name'=>'role[]',
        'value'=>'3'
        );
        ?>
        <?php echo form_label('Editor'); ?>
        <?php echo form_checkbox($data); ?>
    </div>
    <div>
        <?php $data=array(
        'name'=>'role[]',
        'value'=>'4'
        );
        ?>
        <?php echo form_label('Others'); ?>
        <?php echo form_checkbox($data); ?>
    </div>
    <div>
        <?php $data=array(
        'type'=>'submit',
        'value'=>'Create'
        ); ?>
        <?php echo form_submit($data); ?>
    </div>
    <?php echo form_close(); ?>
</body>

First

Set "false" as default value at all those fields.

Second

If you can set only one role per user then you should use radiobox instead checkbox

Third

Capture the parameters sent by the post.

Assuming that you created a route for group/create_group that goes to the data_submitted method

public function data_submitted() {
    $data = array(
    'role' => $this->input->post('role'),
    //more info that you get from the post..
    );
    Now load your model and save it...

}

Put off [] and in your php file logic your can do : $this->input->post("role"); and get value. If you want to do multi-choice's checkbox your must change to an unique name and set value to true for each of them.

Have nice day !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM