我在项目中使用Mapbox,我想知道如何从输入搜索自动完成中获取地址,并使用php在Mysql表中插入该地址?我试图找到与自己的疑问类似的东西,但没有找到。以下是我试图找到答案的一些问题:
在这种情况下,我有一个如下表:
表用户
id | username | address | City
==============================================
01 | John | abc street | New York
02 | Joseph | Marie Street | Washington
03 | Amy | Barkley street | Houston
以我的形式,我有3个字段将数据发送到用户表。下面的HTML代码显示了表单结构:
<form action="" id="register_user">
<label for="username">Username</label>
<input type="text" id="Username" name="Username" placeholder="Your Username..">
<label for="address">address</label>
<input type="text" id="address" name="address" placeholder="Your address..">
<input type="submit" value="Submit" >
</form>
在我的php插入脚本上,我有一个函数,可将输入表单中的值插入用户表:
if(isset($_GET['add_location'])) {
add_location();
}
function add_location(){
$con=mysqli_connect ("localhost", 'root', '','system');
if (!$con) {
die('Not connected : ' . mysqli_connect_error());
}
$username= $_GET['username'];
$address = $_GET['address'];
// Inserts new row with place data.
$query = sprintf("INSERT INTO users " .
" (id, username, address) " .
" VALUES (NULL, '%s', '%s');",
mysqli_real_escape_string($con,$username),
mysqli_real_escape_string($con,$address));
$result = mysqli_query($con,$query);
echo json_encode("Inserted Successfully");
if (!$result) {
die('Invalid query: ' . mysqli_error($con));
}
}
下面的图像恰好显示了我所需要的,但是我不知道该怎么做:
上面可以做这个例子吗?谢谢:)
您可以订阅地址解析器事件,尤其是“结果”,并获取所需的所有信息。
map.addControl(new MapboxGeocoder({
accessToken: accessToken,
mapboxgl: mapboxgl,
}).on('result', function({ result }) { console.log(result.place_name) });