php - 服务器将数据发送到表中

标签 php html mysql arrays json

我有一个简单的服务器发送事件设置,我试图将其显示为表格。目前,我有 SSE php 页面从 MySQL 表中提取数据并将数据作为 Json 数组发送到实时页面。我怎样才能获取 Json 数组并将其放入 html 表中,而不将数组作为帖子发送回来并导致页面“刷新”?

这是我当前的代码

SSE.php

//get MySQL data
$new_data = getVar();
//Create Array
$data_array = array();
//add data to array
while($row = $new_data->fetch_assoc()){
    $data_array[] = $row;
    $out = array_values($data_array);
    $new_out = json_encode($out);
}
//echo the array to html page
echo "data: $new_out \n\n }'";   

索引.html

<script type="text/javascript">
 //check for browser support
 if(typeof(EventSource)!=="undefined") {
 //create an object, passing it the name and location of the server side script
var eSource = new EventSource("send_sse.php");
//detect message receipt
eSource.onmessage = function(event) {
    //write the received data to the page
    //document.getElementById("serverData").innerHTML = event.data;
};

其中 serverData 是当前显示数组的 div

我尝试多次回显 data: 或回显 html 标记,但无法使其在没有 php 运行时错误的情况下工作

谢谢

最佳答案

您可能想要向 PHP 脚本发出 AJAX 请求。这将允许数据库表中的数据填充您的页面而无需刷新。查看this W3Schools example .

您可以使用 jQuery's AJAX functions 来简化用户定义的 JavaScript (尽管您还必须将 jQuery 加载到您的页面上)。

关于php - 服务器将数据发送到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34021685/

相关文章:

javascript - 在滚动上添加 class=active

javascript - 密码恢复链接上不会加载任何内容

mysql - 是否可以编辑或修改内置的 MySQL 函数?

mysql - 从今天早上 5 点开始获取所有 `Timestamp`

未存储 PHP session 数据

php - 如何在两个字段mysql上使用max和min进行搜索?

javascript - 将链接添加到 DfP HTML5 横幅的正确方法是什么?

php - SQLSTATE[HY000] [2002] php_network_getaddresses

PHP/mysql连接

php - 查找表达式中包含 ("") 的所有短语和不包含 ("") 的单词