简体   繁体   中英

AJAX / PHP Reload a part of a page

I have made a RESTful API in PHP. To do shortly, I can register some information by calling an address like http://api.website.com/addInfos

This request is a POST request. Here my method:

File: api.php

<?php

/* Page api.php */

private function addInfos()
{

// Check if it's a POST request and if all fields are correct
// Insert data in my MySQL database

// TODO: make an automatic refresh for page named infos.php

}

?>

Second file, where the data are displayed from my database:

File: infos.php

<?php

/* Page infos.php */

// Connection to database
// Prepare the request using PDO
// Execute the request
// Display infos in a while loop

?>

My question is: How can I refresh the part of code where the data is displayed just after the function named "AddInfos" of my API is called ?

EDIT 1:

Here is what I can do:

File: index.php

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <title></title>

    <script src="js/jquery.js"></script>

</head>

<body>

<div id="infos"></div>

<script>

        function displayInfo(r) { 

            $.post("infos.php",
               { refresh : r},
               function(data){
                 //alert("Data Loaded: " +  data);
                 $("#infos").html(data); 

               }
             );
        } 

        $(document).ready(function() {
            displayInfo(1); 

            $('a.info_link_text').click(function(){
                alert($(this).text());
            });

        });

    </script>

</body>

</html>

File: infos.php

<?php

require_once('database.php');

 if(isset($_POST['refresh'])){

        $selectInfos = getAllInfos();
        $selectInfos->execute();

        echo "<table>";
                    echo "<th>User</th>";
                    echo "<th>Email</th>";

        while($row = $selectInfos->fetch(PDO::FETCH_OBJ)){

                $fullname = $row->user_fullname;
                $email = $row->user_email;

                    echo "<tr>";
                        echo "<td>".$fullname."</td>";
                        echo "<td>".$email."</td>";
                    echo "</tr>";


        }

        echo "</table>";    
}

?>

When I load index.php, I can get the infos from the page infos.php. But I really don't know how can I do this when the method "addInfos" of my API is called, because I need to make a POST request on infos.php (it's OK) but put the result data on index.php (not ok, I don't know how to do that). Please, could you let me know how to achieve this ?

Thank you so much for your help.

Best regards, Lapinou.

I think what you need is a library based on WebSocket HTML5. The server can retain and send notifications to all clients connected at the same time. Then in your javascript handler, you can process anything you want.

In PHP, you could try these examples WebSocket HTML5 PHP on Google

The other way would be to send multiple REST queries on a regular basis to the server to update your page, but it seems you want real-time updates only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM