0

I have this code,when i input invoice code it will get data from MySQL and fill the others text with Ajax. but my ajax don't get return from my controller, any body help me? where I'm wrong?

My controller in PHP to get data.

<?php 
 public function getDetail_transaction($invoice_id){
 $result = $this->db->get_where('tblTransaction', array('payment_trx_id' => $invoice_id))->first_row();
 $data = json_encode($result);
 return $data;
 }
?>

My view:

<div class="container">
 <div class="row">
 <div class="panel panel-default">
 <div class="panel-body">
 <h3>Confirm Your Payment</h3>
 <div class="col-md-4">
 <form name='ConfirmPayment' action="<?php echo base_url('Payment/ConfirmPayment');?>" method="POST" enctype="multipart/form-data">
 <div class="form-group">
 <label>Invoice ID</label> 
 <div>
 <input type="text" class="form-control" name='invoice_id' id='invoice_id' placeholder="Invoice ID" style='width:80%;'/>
 <a type="submit" id='cek_invoice' class="btn cek_invoice">Cek</a>
 </div>
 </div>
 <div class="form-group">
 <label>Nama Rekening Pembayar</label>
 <input type="text" class="form-control" name='user_account_name' id='user_account_name' placeholder="Nama Pembayar">
 </div>
 <div class="form-group">
 <label>Bank Asal</label>
 <input type="text" class="form-control" name='provider_bank' id='provider_bank' placeholder="Nama Pembayar">
 </div>
 <div class="form-group">
 <label>Bank Tujuan</label>
 <input type="text" class="form-control" name='payment_bank_user_name' id='payment_bank_user_name' placeholder="Bank Tujuan">
 </div>
 <div class="form-group">
 <label>No Transafer</label>
 <input type="text" class="form-control" name='no_transaction' id='no_transaction' placeholder="Jumlah di Transfer">
 </div>
 <div class="form-group">
 <label>Transfer Date</label>
 <input type="text" class="form-control" name='transfer_date' id='transfer_date' placeholder="Jumlah di Transfer">
 </div>
 <div class="form-group">
 <label>Deskripsi</label>
 <textarea class="form-control" name='description' id='description' placeholder="Jumlah di Transfer"></textarea>
 </div>
 <div class="form-group">
 <label>Total Transfer</label>
 <input type="text" class="form-control" name='total_transfer' placeholder="No total_transfer">
 </div>
 <div class="form-group">
 <label>Bukti Transaksi (optional)</label>
 <input type="file" name="scan">
 </div>
 <button type="submit" class="btn btn-primary">Submit</button>
 </form>
 </div>
 </div>
 </div>
 </div>

Ajax Script

$(document).ready(function(){
 $("#cek_invoice").click(function(){
 data_invoice = $('#invoice_id').val();
 var url = "<?php echo base_url('Payment/getDetail_transaction/');?>"+'/'+data_invoice;
 $.ajax({
 url : url,
 type: "POST",
 success: function(data) {
 alert(data);
 $('#payment_bank_user_name').val(data.payment_bank_user_name);
 },
 failure: function() {
 alert('fail');
 }
 });
 }); 
});
Nishit Maheta
6,0413 gold badges19 silver badges32 bronze badges
asked Apr 16, 2015 at 6:06
11
  • 1
    use echo $data; instead of return $data; in controller Commented Apr 16, 2015 at 6:07
  • the result {"TransactionID":"863","GuestID":"0","VehicleGroupID":"1","VehicleGroupName":"Kendaraan Penumpang"} but in ajax it doesnt return anything Commented Apr 16, 2015 at 6:10
  • echo json_encode($result); echo it, dont return it. Commented Apr 16, 2015 at 6:12
  • How i can, echo the result in my textbox? Commented Apr 16, 2015 at 6:13
  • after i echo json_encode($result); and then in my ajax, how i can put the data in my textbox? Commented Apr 16, 2015 at 6:14

3 Answers 3

2

you should 'echo' result after encode it! see this:

<?php 
 public function getDetail_transaction($invoice_id){
 $result = $this->db->get_where('tblTransaction', array('payment_trx_id' => $invoice_id))->first_row();
 $data = json_encode($result);
 echo $data; //echo data
 }
?>

in ajax you should parseJSON your data :

$(document).ready(function(){
$("#cek_invoice").click(function(){
 data_invoice = $('#invoice_id').val();
 var url = "<?php echo base_url('Payment/getDetail_transaction/');?>"+'/'+data_invoice;
 $.ajax({
 url : url,
 type: "POST",
 success: function(data) {
 var dun = $.parseJSON(data);
 $('#payment_bank_user_name').val(dun.payment_bank_user_name);
 },
 failure: function() {
 alert('fail');
 }
 });
}); });
Rudolf Gröhling
4,8255 gold badges30 silver badges37 bronze badges
answered Apr 16, 2015 at 6:13

2 Comments

thx, i know the problem why ajax don't get the data from php. but i have new problem, How i can, echo the result from ajax to my textbox?
yes the problem solved thx, but i have new problem, how i can put the data from ajax to textbox?
0

Use

echo $data;exit;

Instead of

return $data;

To display the data (after getting the response) -

var data = $.parseJSON(data);
$('#your_input_field').val(data.payment_bank_user_name);
answered Apr 16, 2015 at 6:16

1 Comment

thx, i know the problem why ajax don't get the data from php. but i have new problem, How i can, echo the result from ajax to my textbox?
0
public function getDetail_transaction($invoice_id){
 $result = $this->db->get_where('tblTransaction', array('payment_trx_id' => $invoice_id))->first_row(); 
 $data = json_encode($result); 
 echo $data;
}

and your javascript edit the below line

success: function(data) { 
 alert(data); 
 var obj = $.parseJSON(data); 
 $('#payment_bank_user_name').val(obj.payment_bank_user_name); 
}, 
Crisoforo Gaspar
3,8402 gold badges23 silver badges27 bronze badges
answered Apr 16, 2015 at 6:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.