【发布时间】:2022-03-04 18:14:27
【问题描述】:
我有 2 个元素。自动搜索栏 和地图(mapbox-gl)
- 我想加载其 id 被搜索的用户的位置。
- 我正在为每个用户获取相同的标记(位置)
- 我正在通过 xmlHttpRequest 从主页(“monitor.php”)加载“get_pin.php”。
- “get_pin.php”有查询
$results=$myPDO->query("SELECT * FROM clients where id =".$user_id);使用此查询,我获取搜索其姓名的人的 id,然后获取该 id 的数据,创建 geojson,这是 get_pin.php 的 json 编码结果。"geometry":{"type":"Point","coordinates":[143.3601,42.6500000000001]},"type":"Feature","properties":[]}
现在,我得到 , SELECT * FROM clients where id = 1,通过硬编码值。所以即使我搜索不同的人的 id。它将标记加载到同一位置。
我试图解决这个问题 - “监视器.php” HTML
<tr>//table appears after the id is searched
<th>id</th>
<td><label id="lbl_id" name="lbl_id">'.$row["id"].'</label></td>
</tr>//further I also get other info of user in the same table
在同一页上-
function load_data(query, typehead_search = 'yes') {
$.ajax({
url: "dbconn.php",
method: "POST",
data: { query: query, typehead_search: typehead_search },
success: function (data) {
//show the table inside employee_data div
$('#employee_data').html(data);
//get the id and store it in js variable
var l_id = document.getElementById('lbl_id').textContent;
alert(l_id);//I am getting the correct id
$.ajax({
type : "POST",
url : "get_pin.php",
data : {l_id : l_id},
success: function(data){
console.log(data);
},
error: function(err){
console.log(err);
}
});
}
});
}
在 get_pin.php 上我正在获取数据
if (isset($_POST["l_id"])) {
require ("db_conn.php");
$user_id =$_POST["l_id"];
try {
$results=$myPDO->query("SELECT * FROM clients where id =".$user_id);
/*get data convert it to a geojson*/
错误- ajax 中的成功函数为我提供了特定 id 的正确 geojson 输出。 但是在我调用 get_pin.php.There 的 xmlhttprequest 中,响应是空白的。 我得到-
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at XMLHttpRequest.request.onload
如何将表中的 id 值传递给 get_pin.php 并在地图上获取该标记?
【问题讨论】:
标签: javascript php jquery ajax xmlhttprequest