Postingan lainnya
Checkbox dalam codeigniter
saya coba ambil data dari 2 checkbox pria & wanita tapi hanya satu saja yang bisa di ambil, saya coba tambahkan jk[] tetapi ada error yang muncul
model Form_m.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Form_m extends CI_Model
{
public function get($id = null)
{
$this->db->from('form');
if($id != null){
$this->db->where('pap_id', $id);
}
$query = $this->db->get();
return $query;
}
public function del($id)
{
$this->db->where('pap_id',$id);
$this->db->delete('form');
}
public function add($post)
{
$params = [
'nama' => $post ['name'],
'jenis_kelamin' => $post['jk']
];
$this->db->insert('form', $params);
}
}
controler Form.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Form extends CI_Controller
{
function __construct(){
parent::__construct();
check_not_login();
$this->load->model('form_m');
}
public function index()
{
$data['row'] = $this->form_m->get();
$this->template->load('template', 'form/form_data',$data);
}
public function add(){
$form = new stdClass();
$form->pap_id = null;
$form->nama = null;
$form->jenis_kelamin = null;
$data = array(
'page' => 'add',
'row' => $form
);
$this->template->load('template', 'form/form_multi',$data );
}
public function process(){
$post = $this->input->post(null, TRUE);
if(isset($_POST['add'])){
$this->form_m->add($post);
}
if($this->db->affected_rows() > 0) {
echo "<script>alert('Data Berhasil di simpan');</script>";
}
echo "<script>window.location='".site_url('form')."';</script>";
}
public function del($id)
{
$this->form_m->del($id);
if($this->db->affected_rows() > 0) {
echo "<script>alert('Data Berhasil di hapus');</script>";
}
echo "<script>window.location='".site_url('form')."';</script>";
}
}
view untuk tampil form_data.php
<section class="content-header">
<h1>
Form_data
<small>Test</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i></a></li>
<li class="active">Data Pap</li>
</section>
<!-- Main content -->
<section class="content">
<div class="box">
<div class="box-header">
<h3 class="box-title">Data PAP</h3>
<div class="pull-right">
<a href="<?= site_url('form/add') ?>" class="btn btn-primary btn-flat">
<i class="fa fa-user-plus"></i> Create
</a>
</div>
</div>
<div class="box-body table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>#</th>
<th>Nama</th>
<th>Jenis Kelamin</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php $no = 1;
foreach ($row->result() as $key => $data) { ?>
<tr>
<td> <?= $no++ ?>.</td>
<td><?= $data->nama ?></td>
<td><?= $data->jenis_kelamin ?></td>
<td class="text-center" width="160px">
<a href="<?= site_url('form/del/'.$data->pap_id) ?>" onclick="return confirm('Apakah anda yakin hapus data?')" class="btn btn-danger btn-xs">
<i class="fa fa-trash"></i> Delete
</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</section>
form untuk tambah data form_multi.php
<section class="content-header">
<h1>
Form Page PAP
<small>Tester</small>
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i></a></li>
<li class="active">PAP</li>
</section>
<!-- Main content -->
<section class="content">
<div class="box">
<div class="box-header">
<h3 class="box-title">Form new </h3>
<div class="pull-right">
<a href="<?= site_url('form') ?>" class="btn btn-warning btn-flat">
<i class="fa fa-undo"></i> Back
</a>
</div>
</div>
<div class="box-body">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<form action="<?=site_url('form/process')?>" method="post">
<div class="form-group" width="10px">
<label> Nama*</label>
<input type="text" name="name" value="<?=$row->nama?>" class="form-control" required>
</div>
<div class="form-group">
<label> Jenis kelamin*</label><br>
<input type="checkbox" name="jk" value="pria" <?=$row->jenis_kelamin?>><i>Pria</i>
<input type="checkbox" name="jk" value="wanita"<?=$row->jenis_kelamin?>><i>Wanita</i>
</div>
<div class="form-group">
<button type="submit" name="<?=$page?>" class="btn btn-success btn-flat">
<i class="fa fa-paper-plane"></i> Save
</button>
<button type="Reset" class="btn btn-warning btn-flat">Reset</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
nama database form ada tabel 'pap_id','nama','jenis_kelamin'
1 Jawaban:
Jawaban Terpilih
untuk kedepannya harap tampikan error nya jika ada error ya gan, biar mempermudah..
pertama klau menurut saya, jk bukannya menggunakan radio button.
solusi nya : ubah name checkbox nya menjadi jk[] dikarenakan jk[] merupakan array jadi nggk bisa langsung masuk ke database, harus dikonversi dulu menggunakan implode() di php contoh nya :
<pre> public function add($post) { $params = [ 'nama' => $post ['name'], 'jenis_kelamin' => implode(',',$post['jk']) ]; $this->db->insert('form', $params); } </pre>
Tanggapan
iyaa mohon maaf gan gak nampilin error, sebenernya emang gak ada error cuma saya pengen tau untuk masukin data nya ke array gimana
makasih banyak !
iya sama sama gan
tapi kalau untuk view datanya biar ke ambil pria & wanita apa perlu di jadikan int value nya ?
maksudnya untuk edit kah ? contoh nya : <input type="checkbox" name="jk[]" value="wanita" <?php if($row->jenis_kelamin=='wanita') {echo "checked";} ?>>Wanita
misal checked nya pria dan wanita hanya perlu di masukan seperti itu aja ?