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.

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
$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 label="SETT NY STATUS">
      <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">
    function refreshUnits(){
        $('#on_duty').load('on_duty.php', function(){
           setTimeout(refreshUnits, 1000);
    function refreshAvail(){
        $('#available_units').load('available_units.php', function(){
           setTimeout(refreshAvail, 1000);

All help is greatly appreciated




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.

People are also looking for solutions to the problem: php - How to get the code from file?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.