skip to Main Content

I’m developing a website using php. I have some problems. I want to know know how to get modified table values while running the php page without refreshing the page.

<html>
<?php
function fun_get_user_name() {
    $host_name = "localhost";
    $db_user_name = "root";
    $password = "";

    $database_name = "database_name";
    $connect = mysqli_connect($host_name, $db_user_name, $password, $database_name);
    $query = "SELECT * FROM `users` ";
    $result = mysqli_query($connect, $query);
    $output = "";
    while ($row = mysqli_fetch_array($result)) {
        $output = $output."<br/>"..$row[0];
    }
}
?>
<script>
function js_function() {
    result = "<?php echo fun_get_user_name; ?>";
    document.getElementById('div_body_users').innerHTML = result;
}
window.setInterval(function() {
    js_function();
}, 1000);
</script>

<body>
    <div id="div_body_users">

    </div>
</body>

</html>

when I made a change in phpmyadmin table the change didn’t affect the page. But I expected the updated table.

3

Answers


  1. So move the fun_get_user_name to another file and then in a setInterval do a n ajax call to that file.

    $.get( "users.php", function( data ) {
      $( ".result" ).html( data );
      alert( "Load was performed." );
    });
    

    For more info on ajax request look at this link
    https://api.jquery.com/jquery.get/

    On user.php you just need to add fun_get_user_name

    Login or Signup to reply.
  2. You can do it with using ajax, here changed with your ajax url and database connection details.

          <html>
          <?php
          function fun_get_user_name()
          {
              $servername = "localhost";
                $username = "root";
                $password = "root";
                $dbname = "test";
    
                // Create connection
              $conn = new mysqli($servername, $username, $password,$dbname);
    
              $sql = "SELECT * FROM users";
              $result = $conn->query($sql);
                if ($result->num_rows > 0) {
                              // output data of each row
                        while($row = $result->fetch_assoc()) {
                                  echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
                          }
                       } else {
                                  echo "0 results";
                   }
    
    
          }
          if($_GET['ajax']==1){
           $data=fun_get_user_name();
    
           echo $data;
           exit(0);
    
          }
          ?>
          <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
          <script>
          $(document).ready(function(){
    
          setInterval(js_function,1000);
    
           function js_function()
           {         
            $.ajax({
                url: "http://localhost/test2/test.php?ajax=1",
                data: '',
                cache: false,
                processData: false,
                contentType: false,
                type: 'POST',
                success: function (result) {  
                     document.getElementById('div_body_users').innerHTML=result;
                }
            });
    
           }
    
          });
          </script>
          <body>
          <div id="div_body_users">
          </div>
          </body>
          </html>
    
    Login or Signup to reply.
  3. You for update page without page refresh you have to use AJAX along with setInterval function.
    Please check below link
    https://www.w3schools.com/asp/asp_ajax_intro.asp

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search