php - 如何从php获取数据到html
问题描述
再会!我想知道如何从 php 获取数据到 html。我是这种代码的新手,你能帮我吗?这是我的代码。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"></script>
<style>
#map {position: absolute; top: 0; bottom: 0; left: 0; right: 0;}
</style>
</head>
<body>
<?php
$mysql_hostname = "localhost";
$mysql_username = "root";
$mysql_password = "root";
$mysql_database = "dtable";
$conn= mysqli_connect($mysql_hostname,$mysql_username,$mysql_password,$mysql_database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, Longitude, Latitude FROM androidtable where Emp_ID = '212' order by Date_log desc";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"]. "</td><td>" . $row["Longitude"] . "</td><td>"
. $row["Latitude"]. "</td></tr>";
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
<div id = "map"></div>
<script>
var map = L.map('map').setView([7.1309155, 125.6402975], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
L.marker([7.1309155, 125.6402975]).addTo(map)
.bindPopup('Employee 212 Location.<br> RealTime Tracker.')
.openPopup();
</script>
</body>
</html>
而不是这个var map = L.map('map').setView([7.1309155, 125.6402975], 13);
到这个var map = L.map('map').setView([$row["Latitude"], $row["Longitude"]], 13);
。我希望你能帮助我解决这个问题。
解决方案
您在代码中使用 JavaScript,因此您无法直接使用 JavaScript 和 PHP 做您想做的事情。您可以使用 AJAX 来执行此操作。这是一个很接近的问题:如何将变量和数据从 PHP 传递到 JavaScript?
您可以使用此链接,祝您有美好的一天。
推荐阅读
- android - 在 android studio 中创建 CSV,以便从项目中获取
- c - 调用以结构为参数的 C 函数
- java - 用java存储聊天消息对象的最佳集合是什么?
- x86 - 将 16 位值的 __m256i 打包(饱和)到 8 位值的 __m128i?
- c++ - while(t-- >0) 是什么意思
- microcontroller - 为什么8051中的程序地址寄存器连接到8位总线?
- mysql - 带有乱码值的mysql表
- c++ - cplex的最优解和cplex的最优解有什么区别?
- c# - c# 将文件内容存储到 MemoryStream 并读回
- flutter - onRefresh 回调返回 null