如何将 PHP 数据从数据库发送回 JQuery 并插入到表中
Posted
技术标签:
【中文标题】如何将 PHP 数据从数据库发送回 JQuery 并插入到表中【英文标题】:How to send PHP data from database back to JQuery and insert to a Table 【发布时间】:2020-09-03 01:25:48 【问题描述】:正如标题所说,我正在尝试根据用户输入显示来自数据库的数据并将其显示在 html 表中。简而言之,我希望用户输入过滤到数据库的查询请求,然后以表格格式显示结果。
我的问题是我不确定如何将查询结果返回给 JQuery,然后将该数据转换为表格以便在 HTML 中显示。
到目前为止,这一直是我的方法,但它不起作用。我已经使用常规 javascript 成功地做到了这一点。但是我想尝试使用 JQuery 来解决这个特殊问题,因为它可以进一步简化代码。
jQuery:
var ourObj = ;
ourObj.data = "Ranges";
ourObj.arPoints = ["SID":TABLID,"T1": T1,"T2": T2,"H1": H1,"H2": H2,"P1": P1,"P2": P2,"C1": C1,"C2": C2,
"Y1": Y1,"Y2": Y2,"M1": M1,"M2": M2,"D1": D1,"D2": D2,"m1": m1,"m2": m2];
$.ajax(
type:"post",
url:"GetQuery.php",
data:"data" : JSON.stringify(ourObj),
success: function(response)
$('SensorData').html(response);
);
PHP
<?php
if (isset($_POST["data"]))
// Decode our JSON into PHP objects we can use
$data = json_decode($_POST["data"]);
$id = $data->arPoints[0]->SID;
$T1 = $data->arPoints[1]->T1;
$T2 = $data->arPoints[1]->T2;
$H1 = $data->arPoints[2]->H1;
$H2 = $data->arPoints[2]->H2;
$P1 = $data->arPoints[3]->P1;
$P2 = $data->arPoints[3]->P2;
$C1 = $data->arPoints[4]->C1;
$C2 = $data->arPoints[4]->C2;
$Y1 = $data->arPoints[5]->Y1;
$Y2 = $data->arPoints[5]->Y2;
$M1 = $data->arPoints[6]->M1;
$M2 = $data->arPoints[6]->M2;
$D1 = $data->arPoints[7]->D1;
$D2 = $data->arPoints[7]->D2;
$m1 = $data->arPoints[8]->m1;
$m2 = $data->arPoints[8]->m2;
// echo "Sensore ID: " . $id;
// echo "Temperature: " . $T1 . " : " . $T2;
// echo "Humidity: " . $H1 . " : " . $H2;
// echo "Pressure: " . $P1 . " : " . $P2;
// echo "CO: " . $C1 . " : " . $C2;
$conn = mysqli_connect('localhost', 'root', '', 'sensors');
if(empty($Y1) || empty($Y2) || empty($M1) || empty($M2) || empty($D1) || empty($D2) || empty($m1) || empty($m2))
$query = "SELECT * FROM sensors, sensorsdata WHERE sensors.SensorID = sensorsdata.SensorID
AND sensorsdata.SensorID = '$id'
AND sensorsdata.Temperature BETWEEN $T1 AND $T2
AND sensorsdata.Humidity BETWEEN $H1 AND $H2
AND sensorsdata.Air_Pressure BETWEEN $P1 AND $P2
AND sensorsdata.Carbon_Monoxide BETWEEN $C1 AND $C2
order by sensorsdata.Date Desc";
else
$time1= mktime(12,$m1,0,$M1,$D1,$Y1);
$time1= date("Y-m-d h:i:s", $time1);
$time2= mktime(12,$m2,0,$M2,$D2,$Y2);
$time2= date("Y-m-d h:i:s", $time2);
$query = "SELECT * FROM sensors, sensorsdata WHERE sensors.SensorID = sensorsdata.SensorID
AND sensorsdata.SensorID = '$id'
AND sensorsdata.Temperature BETWEEN $T1 AND $T2
AND sensorsdata.Humidity BETWEEN $H1 AND $H2
AND sensorsdata.Air_Pressure BETWEEN $P1 AND $P2
AND sensorsdata.Carbon_Monoxide BETWEEN $C1 AND $C2
AND sensorsdata.Date BETWEEN '$time1' AND '$time2'
order by sensorsdata.Date Desc";
// Get Result
$result = mysqli_query($conn, $query);
// Fetch Data
while ($row = mysql_fetch_array($result))
?>
<tr><td><?php echo $row['Temperature']?></td>
<td><?php echo $row['Humidity']?></td>
<td><?php echo $row['Air_Pressure']?></td>
<td><?php echo $row['Carbon_Monoxide']?></td>
<td><?php echo $row['Date']?></td></tr>;
<?php
mysqli_close($conn);
?>
HTML 表格
<table class="table">
<thead>
<tr>
<th>Temperature</th>
<th>Humidity</th>
<th>Air Pressure</th>
<th>Carbon Monoxide</th>
<th>Date</th>
</tr>
</thead>
<tbody id="SensorData">
</tbody>
</table>
【问题讨论】:
我不完全确定你在问什么。您想要更好的方法还是上面的代码不能正常工作? 我已经编辑了问题描述以便更好地理解。但简而言之,我只想将我的 PHP 文件中的查询结果发送回 JQuery,然后将数据插入表中。 您可以尝试 console.logging 您的响应或查看来自您的 php 端点的数据是否显示在网络选项卡中?另外 $('SensorData') 应该是 $('#SensorData') 啊,总是小事,不过我还是设法解决了这个问题。再次感谢那个小错字,我总是想念那些。 【参考方案1】:哇,好的,解决了这个问题,总是小事。
问题 #1:(感谢 Luke T.)将 $(‘SensorData’)
更改为 $(‘#SensorData’)
的简单问题
问题 #2:问题在于我如何浏览 PHP 文件中的每一行数据, 改成
while ($row = mysql_fetch_array($result))
?>
<tr><td><?php echo $row['Temperature']?></td>
<td><?php echo $row['Humidity']?></td>
<td><?php echo $row['Air_Pressure']?></td>
<td><?php echo $row['Carbon_Monoxide']?></td>
<td><?php echo $row['Date']?></td></tr>;
<?php
到
foreach ($result as $row)
?>
<tr><td><?php echo $row['Temperature']?></td>
<td><?php echo $row['Humidity']?></td>
<td><?php echo $row['Air_Pressure']?></td>
<td><?php echo $row['Carbon_Monoxide']?></td>
<td><?php echo $row['Date']?></td></tr>;
<?php
其余部分几乎保持不变,它现在在表格中显示请求的数据,没有问题。
【讨论】:
以上是关于如何将 PHP 数据从数据库发送回 JQuery 并插入到表中的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 AJAX 将 jquery 对象数据发送到 php?
将数据从 android Studio 发送到网站(php)