我在codeigniter中发出了复选框问题。我无法从复选框中获取价值。我想只保存选中复选框的行单元格。所以在控制器中我检查是否选中了复选框。如果选中则保存数据,如果没有继续查看复选框并选中。
请帮我解决这个问题。谢谢你的好意
最诚挚的问候,
这是我的观点
<div class="right_col" role="main">
<div class="">
<div class="page-title">
<div class="title_left">
<h3>Jual Asset Form</h3>
</div>
</div>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="clearfix"></div>
<div class="x_content">
<form class="form-horizontal form-label-left" method="POST" action="<?php echo base_url('assettrn/jualasset'); ?>">
<div class="item form-group">
<label class="control-label col-md-1 col-sm-3 col-xs-12" for="perush">Perusahaan</label>
<div class="col-md-3 col-sm-3 col-xs-12">
<select class="select2_single form-control" tabindex="1" name="perush">
<option value="NULL"></option>
<?php foreach ($perushlov as $key => $value) { ?>
<option value="<?php echo $value['idper']?>"><?php echo $value['namaper']?></option>
<?php } ?>
</select>
</div>
<label class="control-label col-md-1 col-sm-3 col-xs-12" for="lokasi">Lokasi Asset </label>
<div class="col-md-3">
<select class="select2_single form-control" tabindex="-1" name="lokasi">
<option value="NULL"></option>
<?php foreach ($lokasi as $key => $value) { ?>
<option value="<?php echo $value['idlokasi']?>"><?php echo $value['namalokasi']?></option>
<?php } ?>
</select>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-1 col-sm-3 col-xs-12" for="kategori">Kategori </label>
<div class="col-md-3">
<select class="select2_single form-control" tabindex="1" name="kategori">
<option value="NULL"></option>
<?php foreach ($kat as $key => $value) { ?>
<option value="<?php echo $value['idkategory']?>"><?php echo $value['namakategory']?></option>}
<?php } ?>
</select>
</div>
<label class="control-label col-md-1 col-sm-3 col-xs-12" for="manufacture">Manufacture </label>
<div class="col-md-3">
<select class="select2_single form-control" tabindex="1" name="manufacture">
<option value="NULL"></option>
<?php foreach ($assetmanu as $key => $value) { ?>
<option value="<?php echo $value['manufacture']?>"><?php echo $value['manufacture']?></option>
<?php } ?>
</select>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-1 col-sm-3 col-xs-12" for="operan">Umur Asset</label>
<div class="col-md-1 col-sm-12 col-xs-12 form-group">
<select class="select2_single form-control" tabindex="-1" name="operan">
<option value="="> = </option>
<option value=">"> <?php echo ">" ?> </option>
<option value="<"> <?php echo "<" ?> </option>
<option value=">="> >= </option>
<option value="<="> <= </option>
</select>
</div>
<div class="col-md-1 col-sm-12 col-xs-12">
<input id="umurasset" class="form-control col-md-1 col-xs-12" name="umurasset" type="text"></input>
</div>
<label class="control-label" for="umurasset">Tahun</label>
</div>
<div class="item form-group">
<div class="col-md-7 col-sm-6 col-xs-12 col-md-offset-1">
<button type="submit" class="btn btn-info" name="view" value="view">View</button>
</div>
</div>
</div>
<!-- Header Jual -->
<div class="item form-group">
<label class="control-label col-md-1 col-sm-3 col-xs-12" for="keterangan">Keterangan </label>
<div class="col-md-3">
<input id="ketjual" class="form-control col-md-1 col-xs-12" placeholder = "Required : Keterangan Jual Asset" name="ketjual" type="text"></input>
</div>
<label class="control-label col-md-1 col-sm-3 col-xs-12" for="lokasi">Jual Ke PT </label>
<div class="col-md-3">
<select class="select2_single form-control" tabindex="-1" name="namaper">
<option value="NULL"></option>
<?php foreach ($perushlov as $key => $value) { ?>
<option value="<?php echo $value['idper']?>"><?php echo $value['namaper']?></option>
<?php } ?>
</select>
</div>
</div>
<div class="item form-group">
<div class="col-md-7 col-sm-6 col-xs-12 col-md-offset-1">
<button type="submit" class="btn btn-danger" name="jual" value="jual">Jual Asset</button>
</div>
</div>
</div>
<tbody style="font-size: small">
<tbody style="font-size: small">
<?php $cnt; foreach ($asset as $key => $value) { ?>
<tr>
<td><?php echo $cnt ?></td>
<td class="a-center ">
<input type="checkbox" class="flat" value = "<?php echo $value['idasset'] ?>" name="sell[<?php echo $value['idasset'] ?>]">
</td>
<td><?php echo $value['dpj_asset_perush']; ?><input type = 'hidden' name="dpjper[<?php echo $value['idasset'] ?>]" value="<?php echo $value['dpjper'] ?>"></input></td>
<td><?php echo $value['idasset']; ?><input type = 'hidden' name="idasset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['idasset'] ?>" ></input></td>
<td><?php echo $value['kodeasset']; ?><input type = 'hidden' name="kodeasset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['kodeasset'] ?>" ></input></td>
<td><?php echo $value['tglperolehan']; ?><input type = 'hidden' name="tglperolehan[<?php echo $value['idasset'] ?>]" value="<?php echo $value['tglperolehan'] ?>" ></input></td>
<td><?php echo $value['umur_asset']; ?><input type = 'hidden' name="umur_asset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['umur_asset'] ?>" ></input></td>
<td><?php echo $value['namakategory']; ?><input type = 'hidden' name="idket[<?php echo $value['idasset'] ?>]" value="<?php echo $value['idkategory'] ?>" ></input></td>
<td><?php echo $value['nama_asset']; ?><input type = 'hidden' name="nama_asset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['nama_asset'] ?>" ></input></td>
</tr>
<?php $cnt++;} ?>
</tbody>
</table>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
这是我的控制器
public function jualasset()
{
//$data['departement'] = $this->assetmodeltrn->mstdepartment()->result_array();
if (isset($_POST['perush'])){
$idper = $this->input->post('perush'); }
else {
$idper=null;
}
if (isset($_POST['lokasi'])){
$lokasi = $this->input->post('lokasi'); }
else {
$lokasi=null;
}
if (isset($_POST['kategori'])){
$ketagori = $this->input->post('kategori'); }
else {
$ketagori=null;
}
if (isset($_POST['manufacture'])){
$manufacture = $this->input->post('manufacture'); }
else {
$manufacture=null;
}
if (isset($_POST['umurasset'])){
$umurasset = $this->input->post('umurasset');
echo $umurasset;
if (isset($_POST['operan'])){
$operand = $_POST['operan'];
//echo $operand;
} else {
$operand = null;
}
}
else {
$umurasset=null;
$operand=null;
}
//echo $this->$input->post('operan');
if (isset($_POST['jual'])) {
//Klik Jual Asset
$this->form_validation->set_rules('ketjual', 'Keterangan Jual Asset', 'trim|required');
$this->form_validation->set_rules('namaper', 'Name Perusahaan ', 'trim|required');
$this->db1 = $this->load->database('default', true);
$query = $this->db1->query("select nextval('assetjual_seq') header_id");
$row = $query->row();
if (isset($row)) {
$header_id = $row->header_id;
$loguser = $this->session->userdata('userid');
$ketjual=$this->input->post('ketjual');
$idper=$this->input->post('namaper');
$creation_date= date('Y-m-d H:i:s');
$creation_by=$loguser;
$dpj_id = $this->session->userdata('userdpj_id');
$approval = '1';
$object_hdr= array
(
'header_id'=>intval($header_id),
'keterangan'=>$ketjual,
'iddpj'=>$dpj_id,
'idper'=>$idper,
'status'=>$approval,
'creation_date'=>$creation_date,
'creation_by'=>$loguser,
'update_date'=>$creation_date,
'update_date'=>$creation_date,
'update_by'=>$loguser
);
$this->db1 = $this->load->database('default', true);
$queryhdr = $this->db1->insert('tbljual_hdr',$object_hdr);
$line =0;
if ($queryhdr) {
echo 'Insert header Success ';
$object_dtl = array();
// Count distinct entries in the form
//$count = count($this->input->$_POST['tbljual']);
$chkjual = $this->input->post('sell');
$idasset = $this->input->post('idasset');
$kodeasset = $this->input->post('kodeasset');
$idper = $this->input->post('dpjper');
$records = array();
$num_of_rows=count($_POST)-1;
echo count($idasset);
$ids = $this->input->post('kodeasset');
if($ids ){
foreach ($ids as $key => $value){
$j = !empty($chkjual[$value]) ? 1:0;
echo '-' . $j . ', ';
}
}
}
} else {
echo 'Insert Into Jual Detail Failed';
}
}
} else {
$dpj_id = $this->session->userdata('userdpj_id');
$data['username'] = $this->session->userdata('username');
$data['description'] = $this->session->userdata('userdescr');
$data['group_id'] = $this->session->userdata('usergroup');
$data['deptname'] = $this->session->userdata('namadept');
$data['perushlov'] = $this->assetmodel->perushlov()->result_array();
$data['lokasi'] = $this->assetmodeltrn->lokasi()->result_array();
$data['kat'] = $this->assetmodeltrn->listassetkat()->result_array();
$data['cnt']=1;
$data['assetmanu'] = $this->assetmodeltrn->listmanu()->result_array();
$data['asset'] = $this->assetmodeltrn->assetjual($idper, $lokasi, $ketagori, $manufacture, $umurasset,$operand, $dpj_id)->result_array();
$data['content'] = 'transaksi/jualasset';
$this->load->view('template', $data);
}
}
结果如下:
答案 0 :(得分:0)
您的代码仍有问题。
查看更新
<input type="checkbox" class="flat" value = "<?php echo $value['idasset'] ?>" name="sell[<?php echo $value['idasset'] ?>]">
其他隐藏元素名称应该是带键的索引。请参阅以下更新视图。
<tbody style="font-size: small">
<?php $cnt; foreach ($asset as $key => $value) { ?>
<tr>
<td><?php echo $cnt ?></td>
<td class="a-center ">
<input type="checkbox" class="flat" value = "<?php echo $value['idasset'] ?>" name="sell[<?php echo $value['idasset'] ?>]">
</td>
<td><?php echo $value['dpj_asset_perush']; ?><input type = 'hidden' name="dpjper[<?php echo $value['idasset'] ?>]" value="<?php echo $value['dpjper'] ?>"></input></td>
<td><?php echo $value['idasset']; ?><input type = 'hidden' name="idasset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['idasset'] ?>" ></input></td>
<td><?php echo $value['kodeasset']; ?><input type = 'hidden' name="kodeasset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['kodeasset'] ?>" ></input></td>
<td><?php echo $value['tglperolehan']; ?><input type = 'hidden' name="tglperolehan[<?php echo $value['idasset'] ?>]" value="<?php echo $value['tglperolehan'] ?>" ></input></td>
<td><?php echo $value['umur_asset']; ?><input type = 'hidden' name="umur_asset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['umur_asset'] ?>" ></input></td>
<td><?php echo $value['namakategory']; ?><input type = 'hidden' name="idket[<?php echo $value['idasset'] ?>]" value="<?php echo $value['idkategory'] ?>" ></input></td>
<td><?php echo $value['nama_asset']; ?><input type = 'hidden' name="nama_asset[<?php echo $value['idasset'] ?>]" value="<?php echo $value['nama_asset'] ?>" ></input></td>
</tr>
<?php $cnt++;} ?>
<强> PHP 强>
您应该遍历复选框,而不是通过隐藏字段。在这里看到示例循环。
$ids = $this->input->post('sell');
if($ids ){
foreach ($ids as $key => $value){
$j = !empty($kodeasset[$value]) ? 1:0;
}
}
希望这会有所帮助