0

So, I need to develop a function that will invoke when the page is loaded at the start and will extract the value from the database and display the same. The value of cod and bod will keep on updating in the database so what I need is to display that updated value onto a page without reloading or refreshing. Thank you.

code:

<?php
include("dbconfig.db");

?>
<script>

function update_data()
    {
        </script>
        <?php
        $execute_query = "SELECT cod,bod,tss FROM front_end_data WHERE 
        slave_id=1";
        $output=mysqli_query($conn,$execute_query);
        $result_data = mysqli_fetch_assoc($output);

        $cod_data = $result_data['cod'];
        $bod_data = $result_data['bod'];
        $tss_data = $result_data['tss'];

        ?>
        <script>
        setTimeout(update_data, 5000);

    }

window.onload = function() 
{
update_data();
};
</script>

<html>
<head>
<title>
test
</title>
</head>
<body>

<h2>COD DATA:&nbsp;<?php $cod_data ?></h2>
<h2>BOD DATA:&nbsp;<?php $bod_data ?></h2>
<h2>TSS DATA:&nbsp;<?php $tss_data ?></h2>

</body>
</html>
  • Instead of doing above way, you should ajax calls. in which call php file from javascript, in that php file you can use query and return data. – Naveed Ramzan Dec 17 '18 at 05:22

4 Answers4

0

You need Javascript for this. Once the page is loaded, the PHP script has finished executing, so you need to rely on JS for further page interactions after loading.

gdpaz
  • 51
  • 4
0

Your code example is mixing server-side processing with client-side processing. To separate those concerns out, make a single PHP page that serves up the data. Then make a HTML page with javascript that fetches the data from the PHP output. You might like to look at https://www.w3schools.com/php/php_ajax_database.asp as a starting point.

Khyron
  • 468
  • 3
  • 11
0

You need to use ajax for this functionality, You can trigger you ajax call with in a timeframe. But it'll make unnecessory calls to server every time. Instead you can use pusher, You can trigger an event whenever a DB value get updated.

localroot
  • 566
  • 3
  • 13
0

Your Code is mixing Server Side Code with Client Side Code resulting into code vulnerabilities. Instead try doing ajax, and do setTimeout Function. What it does is it will call at every X timestamp and fire AJAX to server side and bring up the data.

Here is the nice example to do that:

https://guide.freecodecamp.org/jquery/jquery-ajax-post-method/
Jaymin
  • 1,643
  • 1
  • 18
  • 39