After my Ajax call is successful, I want to reload my bootstrap-table with the new values that return from my php part.
<table id="gelirtableid" data-toggle="table" data-url="gelir-getdata.php" data-classes="table table-hover" data-striped="true"
data-pagination="true" data-page-list=[20, 40, 75, 100] data-search="true">
<thead>
<tr >
<th data-sortable="true" data-field="tarih">Tarih</th>
<th data-sortable="true" data-field="Toplam">Toplam</th>
</tr>
</thead>
</table>
my php script that fetches data from mysql
<?php
include "dbcon.php";
if($_POST["gelirtablosecimi"]){
$gelirtabloadi = $_POST["gelirtablosecimi"];
$_SESSION["gelirtabloadi"] = $gelirtabloadi;
}
$gelirtabloadi = $_SESSION["gelirtabloadi"];
$gelirgunluktoplam = $db->prepare("select tarih, hasilat + visa + butce_ici + hisse_satis + sosyal_konut + elektrik + haberlesme + iller_bank + diger AS Toplam from $gelirtabloadi");
$gelirgunluktoplam->execute();
$results = $gelirgunluktoplam->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo $json;
?>
and my Ajax call
$("#gelirgetir").click(function() {
var gelirtablosecimi = $("#select1").val();
if (gelirtablosecimi) {
$.ajax({
type: "POST",
url: "gelir-getdata.php",
data: {
"gelirtablosecimi": gelirtablosecimi
},
success: function(result) {
notifyUser('success', 'Başarılı!', 'Tablo başarıyla güncellendi');
location.reload();
},
error: function(result) {
notifyUser('error', 'Hata', 'error');
}
});
} else {
notifyUser('info', 'Dikkat', 'Tablo seçimi yapmadınız!');
}
});
I'm new with Ajax calls, probably the problem is with my ajax part.
As you see, I'm dealing with location.reload();
after the call is successful. I tried reload .container
and #gelirtableid
but nothing worked for me. Basically, when I press the #gelirgetir
button, it updates one of my session values and my table depends on that session value. After the session value changes and if I reload the page, new values show up but I have to force-refresh the page. I only want to refresh the table. Any suggestion?
The error when i get from above solution is about JSON.parse()
. $.each
expects parse the JSON data. Following codes are working like charm.
<table id="gelirtableid" data-toggle="table" data-url="gelir-getdata.php" data-classes="table table-hover" data-striped="true"
data-pagination="true" data-page-list=[20, 40, 75, 100] data-search="true">
<thead>
<tr >
<th data-sortable="true" data-field="tarih">Tarih</th>
<th data-sortable="true" data-field="Toplam">Toplam</th>
</tr>
</thead>
</table>
Ajax call:
$("#gelirgetir").click(function() {
var gelirtablosecimi = $("#select1").val();
if (gelirtablosecimi) {
$.ajax({
type: "POST",
url: "gelir-getdata.php",
data: {
"gelirtablosecimi": gelirtablosecimi
},
success: function(result) {
var content = '';
var obj = JSON.parse(result);
$.each(obj, function(i, data) {
content += '<tr>';
content += '<td>'+obj[i].tarih+'</td>';
content += '<td>'+obj[i].Toplam+'</td>';
content += '</tr>';
});
$('#gelirtableid tbody').html(content);
notifyUser('success', 'Başarılı!', 'Tablo başarıyla güncellendi');
},
error: function(result) {
notifyUser('error', 'Hata', 'error');
}
});
} else {
notifyUser('info', 'Dikkat', 'Tablo seçimi yapmadınız!');
}
});