简体   繁体   中英

Get Data from Controller to Javascript and Pass It Into Modal Codeigniter

please give me some advice so this problem can be solved. So, i have modal, javascript, and controller. The question is :How can i get data from controller in my javascript and then used it into modal in the form of table?

This is my javascript looks like :

 function view_detail(SalesConsID) { $.ajax({ url: "<?php echo site_url('/onpartners/view_detail/')?>/" + SalesConsID, type: "GET", dataType: "JSON", success: function(data) { $('#myModal').modal('show'); $('.modal-title').text('Invoice : ' + SalesConsID); } 

This is my Controller looks like :

$data['data_details'] = $this->M_Onpart->showdetails($id);
echo json_encode($data);

Please guide, thank you so much for your help.

Instead of echo use return .. Then on your ajax. Console.log (data).. after returning you can use your data in on ajax.

//Do something like

let html;

$.each(data, function(i, val){    
 html += `<tr><td>${val.stockname}</td>
 <td>${val.qty}</td>
 </tr>
`;
 $('#modal-content table').append(html);
});

Similary, you can do this with other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM