Can't store data using checkbox to database using codeigniter

55 views Asked by At

I try to store some data using checkbox into my database, but it can't stored. Here is my code:

I try to store some data using checkbox into my database, but it can't stored. Here is my code:

I try to store some data using checkbox into my database, but it can't stored. Here is my code:

View:

<form method="post" action="<?php echo base_url().'igd/igd/input_airborne'?>">
                                <?php echo $this->session->flashdata('message_airborne');?>
                                <input type="text" class="form-control" name="KUNJUNGAN" value="<?php echo $igd->NOMOR ?>" readonly>
                                <input type="datetime-local" class="form-control" name="TANGGAL" value="<?php echo date("Y-m-d H:i:s") ?>">
                                <input type="text" class="form-control" name="OLEH" value="<?php echo $session_user->nip?>" readonly>
                                <input type="text" class="form-control" name="STATUS" value="1" readonly>

<div class="form-check">
                                    <input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck1">
                                    <label class="form-check-label" for="defaultCheck1">
                                        TBC AKtif
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck2">
                                    <label class="form-check-label" for="defaultCheck2">
                                        Campak
                                    </label>
                                </div>
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox" value="" name="DESKRIPSI" id="defaultCheck1">
                                    <label class="form-check-label" for="defaultCheck1">
                                        MDR TB
                                    </label>
                                </div> 
<button class="btn btn-sm btn-success mt-1 mb-1"> Masukkan</button>
                                </form>

Controller:

 public function input_airborne(){
        $DESKRIPSI = $this->input->post('DESKRIPSI');
        $TANGGAL = $this->input->post('TANGGAL');
        $OLEH = $this->input->post('OLEH');
        $STATUS = $this->input->post('STATUS');
        $KUNJUNGAN = $this->input->post('KUNJUNGAN');

        $data = array(
            'DESKRIPSI' => $DESKRIPSI,
            'TANGGAL' => $TANGGAL,
            'OLEH' => $OLEH,
            'STATUS' => $STATUS,
            'KUNJUNGAN' => $KUNJUNGAN
        );
        $this->M_Igd->input_airborne($data, 'igd_inim_airborne');
        $this->session->set_flashdata('message_airborne','<div class="alert alert-success alert-dismissible mt-1 fade show" role="alert">
                                                Airborne berhasil dimasukkan
                                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                <span aria-hidden="true">&times;</span>
                                                </button>
                                                </div>');
        redirect($_SERVER['HTTP_REFERER']);

    }

the DESKRIPSI is field to storing my checkbox entry

2

There are 2 answers

0
IMAM IHSANI On BEST ANSWER

it's been solved, i just change

$DESKRIPSI = $this->input->post('DESKRIPSI');

to

 $DESKRIPSI = implode(', ', $_POST['DESKRIPSI']);

and don't forget to change name to

name="DESKRIPSI[]";

because data will be stored as array

0
KUMAR On

You should specify:-

<input type="checkbox" name="Days[]" value="Daily">Daily<br>

Add [] to all names Days and work at php with this like as array.

After it, you can INSERT values at different columns at db, or use implode and save values into one column.

<html>
<body>
<form method="post" action="chk123.php">
Flights on: <br/>
<input type="checkbox" name="Days[]" value="Daily">Daily<br>
<input type="checkbox" name="Days[]" value="Sunday">Sunday<br>
<input type="checkbox" name="Days[]" value="Monday">Monday<br>
<input type="checkbox" name="Days[]" value="Tuesday">Tuesday <br>
<input type="checkbox" name="Days[]" value="Wednesday">Wednesday<br>
<input type="checkbox" name="Days[]" value="Thursday">Thursday <br>
<input type="checkbox" name="Days[]" value="Friday">Friday<br>
<input type="checkbox" name="Days[]" value="Saturday">Saturday <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

At PHP Server Side Script:-

$checkBox = implode(',', $_POST['Days']);