I have been trying to get a table to refresh without the page being refreshed. I know this can be done with AJAX but I can't seem to get it working.
This is my situation:
I have a main.php page which has an include (table.php). This include contains a table id: tablevisit. This table is dynamically being made: it loops through every row in the database. This is table.php
echo "<div id='tableContainer'>";
$con = new mysqli("localhost","sample","samplepass","sample");
$sql = "SELECT * FROM sampletable";
$i = 0;
$dyn_table = '<table border="1" cellpadding="10" class="visitorlist" id="tablevisit">';
$query = $con->query($sql);
while($row = $query->fetch_assoc()){
$id = $row['ID'];
$name = $row['address'];
$date = $row['date'];
$time = $row['time'];
$url = $row['visit_url'];
$urlstring = $row['visit_url'];
if($i % 3 == 0){
$dyn_table .= '<tr><td>'.$name.'</td>';
$dyn_table .= '<td>'.$date.'</td>';
$dyn_table .= '<td>'.$time.'</td>';
$dyn_table .= '<td class="tdshort"><a href='.$url.' target = "_blank" class=" tdoverflow">' . $_SESSION['cuttedurl'] . '</a></td>';
$dyn_table .= '<td><button type= "button" name = "' . $id . '" class="dynamixbutton navbar-button btn-danger btn" post_id="' . $id . '" type="submit" >CHAT</button></td>';
$dyn_table .= '<td>' . $id . '</td>';
}
else {
$dyn_table .= '<tr><td>'.$name.'</td>';
$dyn_table .= '<td>'.$date.'</td>';
$dyn_table .= '<td>'.$time.'</td>';
$dyn_table .= '<td><a href='.$url.' target = "_blank" class=" tdoverflow">' . $_SESSION['cuttedurl'] . '</a></td>';
$dyn_table .= '<td><button type= "button" name = "' . $id . '" class="dynamixbutton navbar-button btn-danger btn" post_id="' . $id . '" type="submit">CHAT</button></td>';
$dyn_table .= '<td>' . $id . '</td>';
}
$i++;
}
$dyn_table .='</tr></table>';
echo "</div>";
On main.php echo $dyn_table;
is called. Now, $dyn_table needs to be called every x seconds. How does one do this? I've tried just refreshing the table but I thought it would be better to just call $dyn_table every x seconds.
I've tried this, this and this but it did not work out for me. And those were html elements, not PHP variables. Any suggestions? Recommendations, tips?
Thanks in advance!
As far as i understand your question i believe you got no clue on how ajax works. Maybe this might help you.
In pseudo code it looks like this:
whenajaxrequestwassuccessfull (var tabledata = getDataFromServerViaAjax('http://URL of The PHP File which echos a json formated data string')) {
var dataobject = convertjsontoobject(tabledata);
loop over the table {
tablerow->tablefield->setData(dataobject->somedetail)
}
}