简体   繁体   中英

how insert multi value from chechbox to database in codeigniter

I try to insert value from selected checkbox to database with codeigniter, but when i submit the form nothing happen with it this is my view code

<?php if (!empty($BW) && is_array($BW)) {?>
    <div class="form-group has-feedback <?php set_validation_style('P_BW_ID')?>">
    <?php echo form_label('Bandwidth', 'P_BW_ID', array('class' => 'control-label')) ?>
    <div class="panel panel-default">
        <div class="panel-body" style="max-height: 200px;overflow-y:scroll;">
        <?php foreach($BW as $row): ?>
        <?php echo form_checkbox('P_BW_ID',  $row->P_BW_ID,'', FALSE, 'id="bandwidth" class="form-control"') ;
        echo '&nbsp;&nbsp;&nbsp;'.$row->BANDWIDTH;
        ?>
        </br></br>
        <?php endforeach ?>
        <?php set_validation_icon('P_BW_ID') ?>
        <?php echo form_error('P_BW_ID', '<span class="help-block">', '</span>');?>
        </div>
     </div>
    </div>

and this is my controller

public function tambah()
{       
    $id = $this->input->post('P_NW_SERVICE_ID');
    $data['id'] = $id;
    $this->data['serv_name'] = $this->bw_package->getservname();
    $this->data['packname'] = $this->bw_package->getpackname($id);
    $this->data['BW'] = $this->bw_package->getbandwidth();
    $this->data['main_view'] = 'admin/bw_package_form';
    $this->data['form_action'] = site_url('admin/bw_nw_serv_pack/tambah');
    // Data untuk form.
    if (! $_POST) {
       $bw_package = (object) $this->bw_package->default_value;
    } else {
        $bw_package = $this->input->post(null, true);
    }

    // Validasi.
    if (! $this->bw_package->validate('form_rules')) {
        $this->data['values'] = (object) $bw_package;
        $this->load->view($this->layout, $this->data);
        return;
    }
    // Simpan ke DB.
    if ($this->bw_package->tambah($bw_package)) {
        $this->session->set_flashdata('pesan', 'data berhasil disimpan. Kembali ke halaman ' . anchor('admin/bw_nw_serv_pack', 'Bandwidth Package.', 'class="alert-link"'));
        redirect('admin/bw_nw_serv_pack/sukses');
    } else {
        $this->session->set_flashdata('pesan_error', 'data gagal disimpan. Kembali ke halaman ' . anchor('admin/bw_nw_serv_pack', 'Bandwidth Package.', 'class="alert-link"'));
        redirect('admin/bw_nw_serv_pack/error');
    }
}

this my model

public function tambah($bw_pacakage)
{
    $bw_pacakage = (object) $bw_pacakage;
    return $this->insert_batch($bw_pacakage);
}

anyone can help me? thanks before

foreach checkbox array example:

<input type="checkbox" name="example[]" value="red"/>
<input type="checkbox" name="example[]" value="blue"/>
<input type="checkbox" name="example[]" value="brown"/>

To save in your DB convert array to string

implode(',', $this->input->post('example'));

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