php - Adding new lines to table from mysql without refreshing anything
I have a table where I have some results from a SQL table. I'm trying to get new lines to automatically get added to the table without refreshing anything. My problem is that I can only find help for externally fetching results with Ajax, but that resets my table. I have a table, where you can see a name, status, and a message. Everything is fetched from the database except the status, and the user can change the status of a line, but that doesn't get changed in the database, only on screen for the user. What I want to do, is having new lines automatically appear underneath the last line, without having to refresh on the page, or an external page with Ajax. Sorry for the bad explanation, if there is any further questions, please let me know in a comment.
I have already tried using Ajax, but can only find guides on how to externally refresh the page every x seconds.
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"utf8");
$name = mysqli_real_escape_string($conn, strtolower($_POST['navn']));
$sql = "SELECT * FROM users WHERE on_duty='1'";
$result = $conn->query($sql);
while ($row = mysqli_fetch_array($result)) {
if ($row['status'] == "Ledig") {
$row['status'] = "<font color='#9aaf07'><b>Ledig</b></font>";
}
echo '<tr><td>'.$row['id'].'</td>';
echo '<td>'.$row['callsign'].'</td>';
echo '<td><select class="form-control form-control-sm">
<optgroup label="NÅVÆRENDE STATUS">
<option readonly>'.$row['status'].'</option>
</optgroup>
<optgroup label="SETT NY STATUS">
<option>LEDIG</option>
<option>OPPTATT</option>
<option>99</option>
</optgroup>
</select></div></td>
<td><div contenteditable="true">test</div></td>';
}
?>
This is the code echoing the result, and beneath is the code fetching the result
<script type="text/javascript">
$(document).ready(function(){
refreshUnits();
refreshAvail();
});
function refreshUnits(){
$('#on_duty').load('on_duty.php', function(){
setTimeout(refreshUnits, 1000);
});
}
function refreshAvail(){
$('#available_units').load('available_units.php', function(){
setTimeout(refreshAvail, 1000);
});
}
</script>
All help is greatly appreciated
Answer
Solution:
yes, I understand what you want, you can return the row count in the $ count table, pass it as a parameter in the Ajax request, and then run a sql query with Limit clause.