Php 服务器将数据发送到表中
我有一个简单的服务器发送事件设置,我试图将其显示为一个表。目前,我有SSE php页面从MySQL表中提取数据,并将数据作为Json数组发送到live页面。我如何能够获取Json数组并将其放入html表中,而不将该数组作为post发送回并导致页面“刷新” 这是我目前的代码 SSE.phpPhp 服务器将数据发送到表中,php,html,mysql,arrays,json,Php,Html,Mysql,Arrays,Json,我有一个简单的服务器发送事件设置,我试图将其显示为一个表。目前,我有SSE php页面从MySQL表中提取数据,并将数据作为Json数组发送到live页面。我如何能够获取Json数组并将其放入html表中,而不将该数组作为post发送回并导致页面“刷新” 这是我目前的代码 SSE.php //get MySQL data $new_data = getVar(); //Create Array $data_array = array(); //add data to array while($r
//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 }'";
Index.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;
};
//检查浏览器支持
if(typeof(EventSource)!=“未定义”){
//创建一个对象,将服务器端脚本的名称和位置传递给它
var eSource=neweventsource(“send_sse.php”);
//检测消息接收
eSource.onmessage=函数(事件){
//将接收到的数据写入页面
//document.getElementById(“serverData”).innerHTML=event.data;
};
其中serverData是当前显示阵列的div
我曾多次尝试回显数据或回显html标记,但如果没有php运行时错误,我无法使其正常工作
谢谢您可能需要向PHP脚本发出AJAX请求。这将允许数据库表中的数据填充页面而无需刷新。请签出
您可以通过使用简化用户定义的JavaScript(尽管您也必须将jQuery加载到页面上)。实际返回的json是什么样子的?可能您的php返回了无效的json,您可以使用
echo json\u encode(getVar()->fetch\u all())替换上述所有php
@Steve这是目前数据库中的数据数据:[{“ID”:“1”,“Name”:“Ryan”,“City”:“Lake Mary”},`{“ID”:“3”,“Name”:“test”,“City”:“test”},{“ID”:“4”,“Name”:“asdad”,“City”:“asd”},{“ID”:“5”,“Name”:“test:“9”,“Name”:“dsgs”,“City”:“asdss”}]}'
SSE事件的主要内容是所有回音必须以字符串开头,数据:“