These few days I'm stuck with this modal thing.
I have a long list of data.. and in order to reduce too much data display on screen. I plan to use modal so when user wants to see details.. they just click on the particular data and system display all. However, in order to display all data, need to have another function to call. I look around and found that there's no way to call function to populate data and at the same time to open modal. So I try out AJAX & JS.
My problem .. the function didn't work to open modal and display data. APpreciate if you guys out there can help me.. Thanks in advance. Here's are my codes:
Modal: modal-finance.php
defined('BASEPATH') or exit('No direct script access allowed');
class Model_finance extends CI_Model {
public function __construct() {
$this->load->database();
}
// To list HMS data
public function get_hms_data() {
$query = $this->db->select(array(
'c.*',
), false)
->where(
'c.pan is NOT NULL',NULL,FALSE
)
->get('coll_tbl c');
if ($query->num_rows() > 0) {
return $query->result();
}
else {
return false;
}
}
// To list HMS data match with PBB data by PAN No
public function matched_by_pan($pan_no){
$query = $this->db->select(array(
'c.*',
'p.approval_code',
'p.card_no'
), false)
->join('pbb_cc_tbl p', 'p.approval_code = c.approval_code','left')
->like(array(
'c.pan' => $pan_no
))
->get('coll_tbl c');
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
Controller: Finance.php
class Finance extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('model_finance');
}
/* Function to call the view of finance */
public function index() {
$this->load->view('pages/finance');
}
public function get_matchBYpan()
{
$this->load->model('model_finance');
$cardData = $this->input->post('cardData');
$last4pan = substr($cardData,12);
echo $last4pan;
if(isset($last4pan) and !empty($last4pan))
{
$records = $this->model_finance->matched_by_pan($last4pan);
$i=1;
$output = '';
foreach($records as $row)
{
$output .= '
<h4 class="text-center">'.$cardData.'</h4><br>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Booking ID</th>
<th>PAN No</th>
<th>Approval Code</th>
<th>Method</th>
<th>Source</th>
<th>OTA</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>'.$i.'</td>
<td>'.$row->booking_id.'</td>
<td>'.$row->pan.'</td>
<td>'.$row->approval_code.'</td>
<td>'.$row->method.'</td>
<td>'.$row->source.'</td>
<td>'.$row->ota.'</td>
<td>'.$row->total.'</td>
</tr>
</tbody>
</table>';
$i++;
}
echo $output;
}
}
Views: fin_match.php
<!-- modal-tableHMS starts -->
<div id="modal_tableHMS" class="modal fade" tabindex="-1">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header no-padding">
<div class="table-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
<span class="white">×</span>
</button>
HMS data matched by last 4 digit PBB Card number (PAN)
</div>
</div>
<div class="modal-body no-padding">
<div id="hms_result"></div>
</div>
<div class="modal-footer no-margin-top">
<button class="btn btn-sm btn-danger pull-left" data-dismiss="modal">
<i class="ace-icon fa fa-times"></i>
Close
</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal-table -->
</div> <!-- /.main-content-inner -->
<script type="text/javascript">
$(document).ready(function(){
$('.view_data').click(function(){
var cardData = $(this).attr('id');
$.ajax({
url: "<?php echo base_url() ?>Finance/get_matchBYpan",
method: "POST",
data: {cardData:cardData},
success: function(data){
$('#hms_result').html(data);
$('#modal_tableHMS').modal('show');
}
});
});
});
<!-- other codes -->
<div class="row">
<div class="col-xs-6">
<div class="col-xs-12">
<center><h3 class="row header smaller lighter blue">
<span class="col-xs-12"><b> PBB Data </b></span>
</h3></center>
<table id="dataTablePBB" class="table table-bordered table-hover">
<thead>
<tr>
<th>No</th>
<th>Transaction Date</th>
<th>PBB Card Number</th>
<th>Card Type</th>
<th>Approval Code</th>
<th>Currency</th>
<th>Gross Amount</th>
<th>Trace No</th>
</tr>
</thead>
<tbody>
<?php
if(!empty($PBBdata)):
$i=1;
foreach($PBBdata as $row)
{
echo '<tr>';
echo '<td>'.$i.'</td>';
echo '<td>'.$row->trans_date.'</td>';
echo '<td>';
?>
<input type="button" name="view" value="<?php echo $row->card_no; ?>" id="<?php echo $row->card_no; ?>" class="btn btn-info btn-sm view_data"">
<?php
echo '</td>';
echo '<td>'.$row->card_type.'</td>';
echo '<td>'.$row->approval_code.'</td>';
echo '<td>'.$row->gross_cur.'</td>';
echo '<td>'.$row->gross_amt.'</td>';
echo '<td>'.$row->trace_no.'</td>';
echo '</tr>';
$i++;
}
endif;
?>
</tbody>
</table>
</div> <!--col-xs-12-->
</div>
It seems that on your JS script has returning failure. If you are using Chrome you can check it on Console or Network -> XHR there are more detailed information regarding on that error.
And that $output variable must be array because the view needs a storage since it is inside foreach loop and return it like this * echo json_encode($output)* Please see more here.
Related
so im working on this library project where students can order books and admin has to confirm those requests from his side.
here is a pic of the project to have a small idea:
those are students requests
when u click on the blue button you can see more details of the student order:order details
as you can see there is the book that the student has ordered.
now what i wanted is to refresh just the modal after the admin click accept and i did achieve that by using the .load() function here is the piece of code that responsible that :code that load the modal
since we have multiple students and they can order multiple books so i had to use 2 loops that why you see those dynamic id on the modal id and button id, $tmp is for the students loop and $tmp2 is for the books loop. so far so good, the problem is when the modal refresh i lose my script tag which is bad why because all my buttons have click events so no script means buttons will not work here is before and after modal refresh before refresh you can see the script tag that responsible for the buttons and the ajax call here is after as you can see the modal refresh successfully but i lost my script so the buttons will not work so i have to reload the whole page so i can retrieve my script then the buttons will work again.
some of the solutions i tried is to put the script outside of the modal because i assumed .load() can only fetch html no script but i can't because i need the script to be inside that loop in the modal for the books button
here is the modal code if you want more details :
<!-- user books details modal -->
<div class="modal fade" id="userBooksDetails<?= $tmp ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg ">
<div id="modalContent<?= $tmp ?>" class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="btn-close" data-mdb-dismiss="modal" aria-label="Close"></button>
</div>
<div id="modalBody<?= $tmp ?>" class="modal-body">
<table class="table">
<thead>
<tr>
<th scope="col">Picture</th>
<th scope="col">Name</th>
<th scope="col">Status</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php
//dont mind this its just to fetch information of the book to display it in the modal
$livre = new Livre();
$exemplaire = $livre->myBooks($user->id);
if ($exemplaire) {
//here is the second loop because student can order multiple books and each book request need to have an unique id for it button
foreach ($exemplaire as $exem) {
$tmp2--;
$copy = Exemplaire::find($exem['Num_Exemp']);
$book = Livre::find($copy->ISBN);
?>
<script>
$(document).ready(function() {
//request for accepting the order of the user
$("#acceptBook<?= $tmp2 ?>").click(function() {
let copyId = $(this).attr("data-idCopy");
$.ajax({
url: "<?= PROOT ?>emprunts/acceptOrder/" + copyId,
type: "POST",
success: function(data) {
$("#modalContent<?= $tmp ?>").load(window.location.href + " #modalContent<?= $tmp ?>" );
console.log(data)
}
});
});
//request for rejecting the order of the user
$("#rejectBook<?= $tmp2 ?>").click(function() {
let copyId = $(this).attr("data-idCopy");
$.ajax({
url: "<?= PROOT ?>emprunts/rejectOrder/" + copyId,
type: "POST",
success: function(data) {
//$("#userBooksDetails<?= $tmp ?>").load(" #userBooksDetails<?= $tmp ?>");
console.log(data);
$("#modalBody<?= $tmp ?>").load(window.location.href + " #modalBody<?= $tmp ?>" );
}
});
});
//request for returning the book (is not working after reloading on accept fix it )
$("#return<?= $tmp2 ?>").click(function() {
let copyId = $(this).attr("data-idCopy");
$.ajax({
url: "<?= PROOT ?>emprunts/returnBook/" + copyId,
type: "POST",
success: function(data) {
console.log(data);
$("#modalBody<?= $tmp ?>").load(window.location.href + " #modalBody<?= $tmp ?>" );
}
});
})
})
</script>
<tr>
<th> <img src="<?= PROOT . "/public/img/books/" . $book->img ?>" class="w-50 rounded-t-5 rounded-tr-md-0 rounded-bl-md-5" aria-controls="#picker-editor">
</th>
<td class="display-6 text-align-center">
<div class="my-5">
<?= $book->Titre_livre ?>
</div>
</td>
<td>
<div class="form-group mt-4">
<button id="stat<?= $tmp2 ?>" disabled class="btn mt-5"><?php if($exem['isConfirm']==0)
echo "Not Confirmed";
else echo "Confirmed"; ?></button>
</div>
</td>
<td id="test<?= $tmp2 ?>">
<!-- here im checking if the book is confirmed if yes im showing some specific buttons if not hiding some buttons -->
<div class="form-group mt-5">
<a <?php if($exem['isConfirm']==1)
echo "hidden";
else
echo "" ?>
id="acceptBook<?= $tmp2 ?>" class="btn m-2" data-idCopy="<?= $exem['Num_Exemp'] ?>" role="button">Accept</a>
<a <?php if($exem['isConfirm']==1)
echo "hidden";
else
echo "" ?> id="rejectBook<?= $tmp2 ?>" class="btn btn-dark m-2" data-idCopy="<?= $exem['Num_Exemp'] ?>" role="button">Reject</a>
<a <?php if($exem['isConfirm']==0)
echo "hidden";
else
echo "" ?> id="return<?= $tmp2?>" data-idCopy="<?= $exem['Num_Exemp'] ?>" class="btn btn-secondary mt-4"> Return </a>
</div>
</td>
</tr>
<?php }
}
?>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-mdb-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
how do i make my table output appear only when i click on the search button
<?php
require_once 'includes/header.php';
if ($_POST) {
$list = $_POST['categoryList'];
if ( $list != "") {
$sql = "SELECT product.product_name, product.product_image, product.category_id, product.active, category.category_name FROm product
INNER JOIN category on product.category_id = category.category_id
WHERE product.category_id = '".$_POST['categoryList']."' AND product.active = 1";
$result = $connect ->query($sql);
}
/*else{
$msg = echo 'Select category';
}*/
}
?>
HERE i am running a php script to pull data from the database table product
<div class="row">
<div class="col-md-12">
<div class="card mt-5" style="width: 30rem; margin-left: 25%" >
<div class="card-header bg-dark text-light"><span class="glyphicon glyphicon-th-list"></span> Categories </div>
<div class="card-body">
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group row mt-5 ml-5">
<label for="categoryList" class="col-sm-8 col-form-label">Category Name</label>
<div class="col-sm-8">
<select class="form-control" id="categoryList" name="categoryList">
<option value="">--Select--</option>
<?php
$sql= "SELECT category_id, category_name FROM category where category_status = 1 AND category_status = 1";
$result =$connect->query($sql);
while ($row = $result->fetch_array()) {
echo '<option value="'.$row[0].'">'.$row[1].'</option>';
}
?>
</select>
</div>
</div>
<div class="col">
<button type="submit" onclick="myFunction()" class="btn btn-dark" id="searchButton" style="margin-left: 100px">Search </button>
</div>
</form>
</div>
This is the part where i select the categories pulled from the database
</div>
</div>
</div>
<div id="myDiv">
<table class="table" id="myTable">
<thead class="thead thead-dark">
<th>Photo</th>
<th>Name</th>
<th>Category</th>
</thead>
<tbody>
<?php
#$sql = "SELECT product.product_name, product.product_image, product.category_id, product.active, category.category_name FROM product
INNER JOIN category on product.category_id = category.category_id
WHERE product.category_id = '".$_POST['categoryList']."' AND product.active = 1";
$result = $connect ->query($sql);
while($row = $result->fetch_assoc())
{
$imageUrl = substr($row['product_image'], 3);
$name = $row['product_name'];
$category = $row['category_name'];
?>
<tr>
<td><?php echo '<img class="img-round" src='.$imageUrl.' style="height:30px; width:50px;">'?></td>
<td><?php echo $name?></td>
<td><?php echo $category?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
THIS IS THE TABLE THAT DISPLAYS THE PRODUCTS ON IN THE SELECTED CATEGORY ABOVE
<script>
$("#navDashboard").addClass('active');
$("#myTable").DataTable();
</script>
THIS IS THE SCRIPT RESPONSIBLE FOR DATATABLE AND ACTIVE NAVBAR
generally what I am doing is using ajax. give your form an id . for example en ajax call after submitting button
$(document).on('submit', '#show_table', function () {
event.preventDefault();
var formData = new FormData($(this)[0]);
$.ajax({
type: 'POST',
url: 'your_file.php',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: (
function (formData) {
$(".result").html(formData)
}
)
});
});
in the URL pass the name of the file where the call will be made (create a new file). the value from the search input will pass in POST TYPE and then run your query . as you can see the result will display in a div with a class of result. so in the search page create a div
and the table will appear there .
the page will not be refreshed based on the event.preventDefault();
hope this give you any help :)
I am new to laravel. How do I change to users id? I am in process of making delete users as admin. Currently it is using Auth::user(id), which means, it uses id of logged user(admin id). I got stuck, can't figure out how do I change upon clicking on button that it takes users id.
Some of the things are not being used, just standing there.
PagesController.php
public function destroy($id){
return $id;
$user = Auth::user();
$korisnik = User::findOrFail($id);
if ($korisnik->IsAdmin()){
if($korisnik->delete($id)){
return redirect()->back();
}
}else{
if ($user->delete()) {
Auth::logout();
$data = array(
'title' => 'Dobrodošli,',
'title2' => 'da biste nastavili, ulogirajte se!',
);
return view('pages.index')->with($data);
}
}
}
public function action(Request $request)
{
if($request->ajax()){
$output = '';
$query = $request->get('query');
if($query != ''){
$data = DB::table('users')
->where('surname', 'like', '%'.$query.'%')
->orWhere('name', 'like', '%'.$query.'%')
->orWhere('phone', 'like', '%'.$query.'%')
->orderBy('id')
->get();
}else {
$data = DB::table('users')
->orderBy('id')
->get();
}
$total_row = $data->count();
if($total_row > 0){
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->surname.'</td>
<td>'.$row->name.'</td>
<td>'.$row->phone.'</td>
<td><button type="button" class="remove-button btn btn-danger" data-id="'.$row->id.'">
<div class="close">x</div>
</button></td>
</tr>
';
}
}else{
$output = '
<tr>
<td align="center" colspan="5">No Data Found</td>
</tr>
';
}
$data = array(
'table_data' => $output,
'total_data' => $total_row,
);
echo json_encode($data);
}
}
view
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
</div>
<div class="modal-body">
<h2>{{ $modal }}</h2>
</div>
<div class="modal-footer">
<button type="button" class="rem-mod btn btn-secondary" data-dismiss="modal">Zatvori</button>
{{ Form::open(['action'=> ['PagesController#destroy', Auth::user()->id],'method' => 'POST']) }}
{{ Form::submit('Obrišite račun', ['class' => 'bck-mod btn btn-danger']) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
<div class="container">
<div class="panel panel-default">
<div class="panel-heading">Pretraži korisnike</div>
<div class="panel-body">
<div class="form-group">
<input type="text" name="search" id="search" class="form-control" placeholder="Pretraži korisnike" />
</div>
<div class="table-responsive">
<h3 align="center">Broj korisnika: <span id="total_records"></span></h3>
<table id="users" class="table table-striped table-bordered">
<thead>
<tr>
<th>Prezime</th>
<th>Ime</th>
<th>Telefon</th>
<th></th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
fetch_customer_data();
function fetch_customer_data(query = ''){
$.ajax({
url:"{{ route('live_search.action') }}",
method:'GET',
data:{query:query},
dataType:'json',
success:function(data)
{
$('tbody').html(data.table_data);
$('#total_records').text(data.total_data);
}
})
}
//
$(document).on('keyup', '#search', function(){
var query = $(this).val();
fetch_customer_data(query);
});
$('#users').on('click', '.remove-button', function(){
var id=$(this).data('id');
$("#exampleModal").modal("show");
console.log(id);
});
$(document).on('click', '.remove-button', function(){
var id = $(this).attr('id');
{
$.ajax({
url:"{{route('live_search.destroy')}}",
method:"get",
data:{id:id},
success:function(data)
{
alert(data);
$('#users').DataTable().ajax.reload();
}
})
}
});
});
try below logic,
<td><button type="button" class="remove-button btn btn-danger" data-action="{{route('nameofroute',$row->id)}}" > </td>
Set in form Using jQuery like:
$(".remove-button ").click(function(){
$("#formId").attr('action',$(this).attr('data-action')));
})
I have an HTML table that loads its values dynamically through a PHP snippet with SQL sentences. There is also a Select and a button to add a row to the table. This button is listened by an ajax procedure to send the data to the php and to insert the new row as a record in the database. I want of course the table (not all the page) refresh the information. For that I use the jquery ($).load function. Everything is working good, the database is updated, but when the HTML table refresh it appears empty. What I am doing wrong?
Here is the HTML table:
<!-- Procedure Table-->
<div id=idProcedimientos class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<div class="x_title">
<h2>Procedures <small></small></h2>
<div class="clearfix"></div>
</div>
<div class="x_content">
<div class="row">
<div class="col-sm-12">
<div class="card-box table-responsive">
<div class="form-group">
<div class="col-md-2 col-sm-6 col-xs-12">
<? $qProcedure = "SELECT * FROM Procedures";
$resultsProcedure = mysqli_query($db, $qProcedure);
echo "<select id='idSelected_procedure' name='selected_procedure' class='form-control'>";
while ($rowProcedure = mysqli_fetch_assoc($resultsProcedure)) {
echo"<option value ='{$rowProcedure['IdProcedure']}'>{$rowProcedure['Name']}</option>";
};
echo "</select><br />";
?>
</div>
</div>
<div class="form-group">
<div class="col-md-12 col-sm-6 col-xs-12">
<button type="submit" id="idBttnProcedure-add" class="bttnProcedure-add btn btn-round btn-primary">Add Procedure</button><br /><br />
</div>
</div>
<div id="idProcedureTable" class="table-editable">
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>Procedure</th>
<th>Date</th>
<th>Ok</th>
<th>Summary</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
<?
$queryProcedure = "SELECT FoalingsP.IdFoaling, Procedures.Name, JournalProcedures.IdJournalFk, FoalingsP.Date, FoalingsP.Ok,
FoalingsP.Summary FROM FoalingsP, Procedures, JournalProcedures
WHERE
JournalProcedures.IdJournalFk=$row[IdJournal] AND
JournalProcedures.IdJournalProcedure=FoalingsP.IdJournalProcedureFk AND
JournalProcedures.IdProcedureFk=Procedures.IdProcedure
UNION
SELECT UltrasonographiesP.IdUltrasonography, Procedures.Name, JournalProcedures.IdJournalFk, UltrasonographiesP.Date,
UltrasonographiesP.Ok, UltrasonographiesP.Summary FROM UltrasonographiesP, Procedures, JournalProcedures
WHERE
JournalProcedures.IdJournalFk=$row[IdJournal] AND
JournalProcedures.IdJournalProcedure=UltrasonographiesP.IdJournalProcedureFk AND
JournalProcedures.IdProcedureFk=Procedures.IdProcedure";
$resultsProcedure = mysqli_query($db, $queryProcedure);
while ($rowProcedure = mysqli_fetch_assoc($resultsProcedure)){
echo " <tr><td>{$rowProcedure['Name']}</td>
<td>{$rowProcedure['Date']}</td>";
if($rowProcedure["Ok"]=="1") {echo "<td contenteditable='true'><label><input type='checkbox' checked></label></td>";};
if($rowProcedure["Ok"]=="0") {echo " <td contenteditable='true'><label><input type='checkbox'></label></td>";};
echo "<td>{$rowProcedure['Summary']}</td>
<td><span id='tableProcedure-remove' class='tableProcedure-remove table-remove glyphicon glyphicon-remove'></span></td></tr>";
};
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /Procedure Table-->
And here is the script:
<script>
$('.bttnProcedure-add').click(function () {
var idJournal = "<?php echo $row[IdJournal]; ?>";
var idSelected_procedure = document.getElementById("idSelected_procedure").value;
alert(idSelected_procedure);
$.ajax({
type: "POST",
url: "server.php",
data: {mare_idJournal:idJournal,
mare_idSelected_procedure:idSelected_procedure
},
success: function(msg){
alert("Everything saved correctly");
$("#datatable-responsive").load("control_horse_journal.php #datatable-responsive");
}
});
return false;
});
</script>
Thank you very much.
Thanks to #epascarello for the advice. I got the solution. I discarded .load function, and instead I appended a row directly with .after .The final script is like this:
<script type="text/javascript">
$('.bttnProcedure-add').click(function () {
var idJournal = "<?php echo $row[IdJournal]; ?>";
var idSelected_procedure = document.getElementById("idSelected_procedure").value;
var $TABLE_PROCEDURES = $('#idProcedureTable');
var selectedProcedure = document.getElementById("idSelected_procedure");
var name = selectedProcedure.options[selectedProcedure.selectedIndex].text;
var rowP="<tr>"
+"<td>" +name+"</td>"
+"<td>"+"0000-00-00 00:00:00"+"</td>"
+"<td contenteditable='true'><label><input type='checkbox'></label></td>"
+"<td>"+""+"</td>"
+"<td><span id='tableProcedure-remove' class='tableProcedure-remove table-remove glyphicon glyphicon-remove'></span></td>"
+"</tr>";
$('#bodyTbProcedure tr:last').after(rowP);
$.ajax({
type: "POST",
url: "server.php",
data: {mare_idJournal:idJournal,
mare_idSelected_procedure:idSelected_procedure
},
success: function(msg){
alert("Everything saved correctly");
//$("#datatable-responsive").load("control_horse_journal.php #datatable-responsive");
}
});
return false;
});
</script>
I'm working with codeigniter, and i want to show the detail data in modal, the detail data is from another table.
I have a button like this:
<button class="btn btn-info" onclick="detail_barang("<?php echo $tbrang->id_barang;?>")">Detail</button>
And this is my detail_barang function:
function detail_barang(id)
{
$('#form')[0].reset(); // reset form on modals
$.ajax({
url : "<?php echo site_url('admin/Barang/barang/ajax_detail_barang/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('#modal_detail').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Detail Barang'); // Set title to Bootstrap modal title
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Kesalahan!');
}
});
}
Modal Script:
<div class="modal fade" id="modal_detail">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 class="modal-title">Detail Barang</h3>
</div>
<div class="modal-body">
<table class="table table-striped" id="tblGrid">
<thead id="tblHead">
<tr>
<th>ID Barang</th>
<th>No Inv</th>
<th class="text-right">Kondisi</th>
</tr>
</thead>
<tbody>
<?php
foreach($detail_barang as $detil)
{?>
<tr>
<td><?php echo $detil->id_barang ?></td>
<td><?php echo $detil->no_inv ?></td>
<td><?php echo $detil->kondisi ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default " data-dismiss="modal">Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
ajax_detail_barang function:
public function ajax_detail_barang($id)
{
$where = array
(
'id_barang' => $id,
'kondisi' => 'Ada'
);
$data['detail_barang'] = $this->modelku->get_by_id('detail_barang', $where)->result();
echo json_encode($data);
}
get_by_id function:
public function get_by_id($table, $where)
{
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
return $query->row();
}
But when i click the button, the error appears "Kesalahan!" whats wrong my code?
The problem i see is in how u are using the returned data from ur model
public function get_by_id($table, $where)
{
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
return $query->row();
//at this point it already contains the data u need
}
but here u are using result();
$data['detail_barang'] = $this->modelku->get_by_id('detail_barang', $where)->result();
//i tried and u cant use result method if u already executed row method.
In short, remove result()
Hope my answer helps u.