user3018000 user3018000 - 4 months ago 23
Ajax Question

Implementing ajax functionality using Mysqli And PHP and jQuery

How can I use ajax php and mysqli to read data from my database
without using any button?
When I add a row to the table, I want information to be appear on page without loading/refreshing the page or click a button. Can I do that?

And this information will be appear like the code below

php code

<?php
if (isset($_GET['id'])) {
$id = intval($_GET['id']);
$date = date("l jS \of F Y");
$query = mysqli_query($conn, "select * from tbl_dailyprogress where db_activityid='$id' and db_d='1'")or die(mysqli_error($conn));
$count = mysqli_num_rows($query);
if ($count != 0) {
echo"<table class='ol-md-12 table-bordered table-striped table-condensed cf table-bordered' id='alternatecolor'>";
echo"<thead class='cf'>";
echo"<tr>";
echo" <th style='background:#f7ac01;font-size:16px;vertical-align: middle;text-align:center'rowspan='2' >Date</th>
<th style='background:#f7ac01;font-size:16px;vertical-align: middle;text-align:center'rowspan='2' >Notes</th>
<th style='background:#f7ac01;font-size:16px;vertical-align: middle;text-align:center'rowspan='2' >Progress %</th>
";
echo"</tr></thead><tbody>";
while ($row = mysqli_fetch_array($query)) {
$idp = $row['db_id'];
$date = $row['db_date'];
$note = $row['db_note'];
$progress = $row['db_progress'];
echo"<tr>";
echo"<td data-title='Date'>";
echo $date;
echo"</td>";
echo"<td data-title='Note'>";
echo $note;
echo"</td>";
echo"<td data-title='Progress %'>";
echo'
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:' . $progress . '%">
' . $progress . '%
</div>
</div>';
echo"</td>";
}
echo"</tr></tbody>";
echo"</table>";
echo"<br/>";
}?>

Answer

Simple example, lest assume that you what to load you data in container div tag <div id="container"></div> from page my_data.php

$(document).ready(function(){
    $("#container").load("my_data.php");
});
Comments