Achmad Rifanni
Achmad Rifanni

Reputation: 15

Update checkbox Codeigniter

My Controller :

public function update($id)
{
    $data['tag'] = $this->Tag_model->get_all();
    $data['gambar'] = $this->Gambar_model->get_by_id($id);
    $this->load->view('gambar/gambar_edit', $data);
}

public function update_action()
{
    $this->_rules();

    if ($this->form_validation->run() == FALSE) {
        $this->update($this->input->post('id_gambar'));
    } else {
        $data = array(
            'judul_gambar' => $this->input->post('judul_gambar', TRUE),
            'judul_tag' => implode(', ', $this->input->post('judul_tag', TRUE)),
            'img_gambar' => $this->input->post('img_gambar', TRUE)
         );

         $this->Gambar_model->update($this->input->post('id_gambar'), $data);
         $this->session->set_flashdata('flash', 'diubah');
         redirect('gambar');
    }
}

My View :

<?= form_open('gambar/update_action') ?>
  <div class="form-group col-lg-8">
    <input type="hidden" class="form-control" id="idgambar" name="id_gambar" value="<?= $gambar['id_gambar'] ?>">
    <label for="judulgambar">Nama</label>
    <input type="text" class="form-control" id="judulgambar" name="judul_gambar" value="<?= $gambar['judul_gambar'] ?>">
    <small class="form-text text-danger font-italic"><?= form_error('judul_gambar') ?></small>
  </div>
  <div class="form-group col-lg-8">
    <label for="imggambar">Gambar</label>
    <input type="text" class="form-control" id="imggambar" name="img_gambar" value="<?= $gambar['img_gambar'] ?>">
    <small class="form-text text-danger font-italic"><?= form_error('img_gambar') ?></small>
  </div>
  <div class="form-group pl-3">
    <?php foreach ($tag as $row): ?>
      <div class="form-check-inline">
          <input class="form-check-input" type="checkbox" name="judul_tag[]" value="<?= $row['judul_tag'] ?>">
          <label class="form-check-label" for="gridCheck">
            <?= $row['judul_tag'] ?>
          </label>
      </div>
    <?php endforeach; ?>
    <small class="form-text text-danger font-italic"><?= form_error('judul_tag[]') ?></small>
  </div>
  <button type="submit" class="btn btn-success ml-3" name="submit">Ubah</button>
<?= form_close() ?>

I want to ask how I can get checked on my checkbox was submitted when I want to do update. Update function is working but I just get value on input text not for checked checkbox, so when I'm doing update, the checkbox is empty value and I must check it again. I was search it's must be explode the value but I don't know where it supposed to be. I very appreciate it for your help, thanks

Upvotes: 1

Views: 93

Answers (1)

MD. Jubair Mizan
MD. Jubair Mizan

Reputation: 1560

You have to use explode() first as you use implode() while updating the rows . After that use in_array() to find the row

For more details.

in_array() explode()

<?= form_open('gambar/update_action') ?>
  <div class="form-group col-lg-8">
    <input type="hidden" class="form-control" id="idgambar" name="id_gambar" value="<?= $gambar['id_gambar'] ?>">
    <label for="judulgambar">Nama</label>
    <input type="text" class="form-control" id="judulgambar" name="judul_gambar" value="<?= $gambar['judul_gambar'] ?>">
    <small class="form-text text-danger font-italic"><?= form_error('judul_gambar') ?></small>
  </div>
  <div class="form-group col-lg-8">
    <label for="imggambar">Gambar</label>
    <input type="text" class="form-control" id="imggambar" name="img_gambar" value="<?= $gambar['img_gambar'] ?>">
    <small class="form-text text-danger font-italic"><?= form_error('img_gambar') ?></small>
  </div>
  <div class="form-group pl-3">
    <?php 
    foreach ($tag as $row): 
      $explodeRow = explode(',',$row['judul_tag']);
    ?>
      <div class="form-check-inline">
          <input class="form-check-input" type="checkbox" name="judul_tag[]" <?php if(in_array($row['judul_tag'],$explodeRow)) echo 'checked'; ?> value="<?= $row['judul_tag'] ?>">
          <label class="form-check-label" for="gridCheck">
            <?= $row['judul_tag'] ?>
          </label>
      </div>
    <?php endforeach; ?>
    <small class="form-text text-danger font-italic"><?= form_error('judul_tag[]') ?></small>
  </div>
  <button type="submit" class="btn btn-success ml-3" name="submit">Ubah</button>
<?= form_close() ?>

Upvotes: 1

Related Questions