Postingan lainnya
Mengambil value nama client berdasarkan dropdown yang dipilih. di codeigniter
saya ingin mengambil value nama client berdasarkan dropdown client yang saya pilih, namun saat saya insert data, nama client nya tidak muncul dan tidak terinput.
controller..
public function jual($id)
{
$data['id'] = $id;
$data['jual'] = $this->M_newdevice->jual($id);
$data['client'] = $this->M_newdevice->client();
$this->load->view('admin/_partials/jual', $data);
}
public function getclient_name()
{
$user_id = $this->input->post('id');
$txtHint3 = $this->M_newdevice->client_name($user_id);
$listss = "<option value=''>Pilih</option>";
foreach($txtHint3 as $data){
$listss .= "<option value='".$data->id."'".$data->name."></option>"; }
$callback = array('list_nama'=>$listss);
echo json_encode($callback);
}
public function action_jual($id ='')
{
$client_id = $this->input->post('client_id');
$data = array(
'client_id' => $client_id,
'client_name' => $this->input->post('client_name'),
);
$this->db->insert('dev_out', $data);
return redirect('admin/Pengaturan_barang/pengeluaran_barang');
}
model..
public function client()
{
$this->db2->select('*');
$this->db2->from('ost_user__cdata');
$this->db2->join('ost_user','ost_user.id=ost_user__cdata.user_id');
$this->db2->order_by('ost_user.id','desc');
$query = $this->db2->get();
return $query->result();
}
public function client_name($user_id)
{
$this->db2->select('*');
$this->db2->from('ost_user');
$this->db2->where('ost_user.id',$user_id);
$query = $this->db2->get();
return $query->result();
}
view..
<?php foreach ($jual as $row){ ?>
<form class="form-horizontal" method="post" action="<?php echo base_url(); ?>admin/pengaturan_barang/action_jual/<?php echo $row->id ?>">
<?php } ?>
<div class="control-group">
<label class="control-label" for="inputEmail" placeholder="Device Status" >Client</label>
<div class="controls">
<select name="client_id" class="chzn-select" id="txtHint2" required>
<option value="">Client:</option>
<?php foreach ($client as $row){ ?>
<option value="<?php echo $row->id;?> Name <?php echo $row->name; ?>"><?php echo $row->id_pelanggan.' '.$row->name; ?></option>
<?php }?>
</select>
</div>
</div>
<div><input type="hidden" name="client_name" id="txtHint3"></div>
<button id="update" data-placement="right" title="Click to update" name="update" type="submit" class="btn btn-info"><i class="icon-save icon-large"></i> Jual</button>
</div>
</div>
</form>
<link rel="stylesheet" type="submit">
<script>
$(document).ready(function(){
$("#loading").hide();
$("#txtHint2").change(function(){
$("#txtHint3").hide();
$("#loading").hide();
$.ajax({
type: "POST",
url: "<?php echo base_url("admin/pengaturan_barang/getclient_name"); ?>",
data: {id : $("#txtHint2").val()},
dataType: "json",
beforeSend: function(e) {
if(e && e.overrideMimeType) {
e.overrideMimeType("application/json;charset=UTF-8");
}
},
success: function(response){
$("#loading").hide();
$("#txtHint3").html(response.list_nama).show();
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status + "\n" + xhr.responseText + "\n" + thrownError);
}
});
});
});
</script>
4 Jawaban:
<pre> $("#txtHint2").val() // coba ganti dengan $(this).val() </pre>
sudah saya coba rubah, tapi tidak ada perubahan. saat saya inspect sebenernya value nya sudah jalan,nama client berdasarkan id yang dipilih sudah muncul, namun saat kita eksekusi jual, nama client nya tidak muncul dan tidak create di database.
<img src='https://i.imgur.com/a9AKPwg.jpg'>
masukin ke foreachnya formatnya kaya gini <pre> <option onclick="ambilNama('<?php echo $row->id ?>', '<?php echo $row->nama ?>')" > ... ... ... <script> function ambilNama(id, nama){ console.log(id); console.log(nama); // kalo udh berhasil, masukin ajax agan disini } </script>
</pre>
ane lupa onclick ato onchange, atur atur saja
Tanggapan
itu foreach nya letak di mana gan? s
di optionnya, tapi yg scriptnya tarok di bawah jangan dimasukin ke foreach
saya buat foreach nya di controller pakai json. bisa langsung contohin ngga gan di controllernya gmna? saya masih newbie soalnya hehe.
sama contoh ajax nya. saya bingung apa yang ditambah dan diganti
Jawaban Terpilih
sudah solved.
saya cuma tambah query di action jualnya aja. kan client id nya sudah terkirim. jadi tinggal ditambah query untuk nampilin nama client nya berdasarkan client id yang sudah terkirim tadi.
controller <pre> public function action_jual($id ='') { $client_id = $this->input->post('client_id'); $client_name = $this->M_newdevice->client_name($client_id); $data = array( 'client_id' => $client_id, 'client_name' => $client_name ); $this->db->insert('dev_out', $data); return redirect('admin/Pengaturan_barang/pengeluaran_barang'); } </pre>
model M_newdevice <pre> public function client_name($client_id) { $this->db2->select('name')->from('ost_user')->where('id',$client_id); $query = $this->db2->get();
if ($query-&gt;num_rows() &gt; 0) {
return $query-&gt;row()-&gt;name;
}
return false;
}
</pre>
view
<pre>
<?php foreach ($jual as $row){ ?>
<form class="form-horizontal" method="post" action="<?php echo base_url(); ?>admin/pengaturan_barang/action_jual/<?php echo $row->id ?>">
<?php } ?>
<div class="control-group">
<label class="control-label" for="inputEmail" placeholder="Device Status" >Client</label>
<div class="controls">
<select name="client_id" class="chzn-select" id="txtHint2" required>
<option value="">Client:</option>
<?php foreach ($client as $row){ ?>
<option value="<?php echo $row->id;?> Name <?php echo $row->name; ?>"><?php echo $row->id_pelanggan.' '.$row->name; ?></option>
<?php }?>
</select>
</div>
</div>
<button id="update" data-placement="right" title="Click to update" name="update" type="submit" class="btn btn-info"><i class="icon-save icon-large"></i> Jual</button> </div> </div> </form> <link rel="stylesheet" type="submit"> </pre>
Semoga bermanfaat untuk temen2 yang punya problem yang sama.