简体   繁体   English

AJAX / PHP重新加载页面的一部分

[英]AJAX / PHP Reload a part of a page

I have made a RESTful API in PHP. 我在PHP中制作了RESTful API。 To do shortly, I can register some information by calling an address like http://api.website.com/addInfos 为此,我可以通过调用http://api.website.com/addInfos之类的地址来注册一些信息

This request is a POST request. 该请求是POST请求。 Here my method: 这是我的方法:

File: api.php 档案: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 文件: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 ? 我的问题是:在调用我API的名为“ AddInfos”的函数之后,如何刷新显示数据的代码部分?

EDIT 1: 编辑1:

Here is what I can do: 这是我可以做的:

File: index.php 文件: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 文件: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. 当我加载index.php时,我可以从页面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). 但是我真的不知道在调用API的方法“ addInfos”时该怎么做,因为我需要在infos.php上发出POST请求(没关系),但是将结果数据放在index.php上(不好,我不知道该怎么做)。 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. 我认为您需要的是基于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. 然后,您可以在javascript处理程序中处理所需的任何内容。

In PHP, you could try these examples WebSocket HTML5 PHP on Google 在PHP中,您可以尝试以下示例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. 另一种方法是定期向服务器发送多个REST查询以更新您的页面,但是看来您只需要实时更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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