hi guys i have a issue on ajax. where ajax can't work to take the values of second rows. |
this's my code in controller
- function getdata_barang($barcode) {
- $this->db1->where("barcode" ,$barcode);
- $query = $this->db1->get('tblmstbarang');
- if($query->num_rows()>0) {
- $data= array();
- foreach($query->result() as $row)
- {
- $data['barang_nama'] = $row->barang_nama;
- $data['harga_beli']= $row->harga_beli;
- echo json_encode($data);
- }
- } else{
- $data['barang_nama'] = "";
- $data['harga_beli']= "";
- echo json_encode($data);
- }
- }
- function getdata_barang(){
- $barcode = $this->input->post('barcode');
- $data=$this->Mbarang->getdata_barang($barcode); }
this's my code in view
- <script type="text/javascript">
- $(document).ready(function() {
- $('.form-control#barcode_2').keyup(function() {
- var barcode = $(this).val();
- $.ajax({
- type : "post",
- data : "barcode=" + barcode,
- url : "<?php echo base_url();?>index.php/master/barang/Cbarang/getdata_barang",
- dataType: "json",
- success: function(data){
- $('.form-control#barang_nama_2').val(data.barang_nama);
- $('.form-control#harga_beli_2').val(data.harga_beli);
- }
- });
- });
- });
- </script>
- <td><input type="text" class="form-control" id="barcode" name="barcode[]" value="<?php echo set_value('barcode['.$i.']') ;?>" onKeyUp="getValues()" ></td>
- <td><input type="text" class="form-control" id="barang_nama" name="barang_nama[]" value="<?php echo set_value('barang_nama['.$i.']') ;?>" onKeyUp="getValues()" disabled></td>
- <td><input type="text" class="form-control" id="harga_beli" name="harga_beli[]" value="<?php echo set_value('harga_beli['.$i.']');?>" onKeyUp="getValues()" disabled></td>
- <td><input type="text" class="form-control" id="barcode_2" name="barcode[]" value="<?php $a=set_value('barcode[0]') ; echo $a;?>"></td>
- <td><input type="text" class="form-control" id="barang_nama_2" name="barang_nama[]" value="<?php $a=set_value('barang_nama[0]') ; echo $a;?>" onKeyUp="getValues()" readonly></td>
- <td><input type="text" class="form-control" id="harga_beli_2" name="harga_beli[]" value="<?php $a=set_value('harga_beli[0]'); echo $a;?>" onKeyUp="getValues()" readonly></td>
- <td><input type="text" class="form-control" id="barcode_3" name="barcode[]" value="<?php echo $detail->barcode;?>"></td>
- <td><input type="text" class="form-control" id="barang_nama_3" name="barang_nama[]" value="<?php echo $detail->barang_nama;?>" onKeyUp="getValues()" readonly></td>
- <td><input type="text" class="form-control" id="harga_beli_3" name="harga_beli[]" value="<?php echo $detail->harga_beli;?>" onKeyUp="getValues()" readonly></td>