gpt4 book ai didi

javascript - 基于 Javascript 函数值,通过 PHP 检索数据库数据

转载 作者:行者123 更新时间:2023-11-28 06:34:55 25 4
gpt4 key购买 nike

我使用 GET 获取该文件的地名。然后我将地名转换为纬度和经度。

我想根据纬度和经度显示数据库中的一些数据,但我无法将纬度和经度变量从 JS 发送到 PHP。

我也尝试过 Ajax,但这也不起作用,因为 PHP 在脚本之前加载。如果我错了请纠正我。

任何形式的帮助将不胜感激。

<?php
$user_latitude
$user_longitude
$result=$con->query("SELECT *, ( 6371 * acos( cos( radians($user_latitude) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians($user_longitude) ) + sin( radians($user_latitude) ) * sin( radians( lat ) ) ) ) AS distance FROM Location HAVING distance < 100 ORDER BY distance LIMIT 0 , 10");

for($x=0;$x<$result->num_rows;$x++){
$row=$result->fetch_assoc();
echo "<div class='result-data'>";
echo $row["name"];
echo "</div>";
}
?>
<script>
$(function() {
geocoder = new google.maps.Geocoder();
alert($_GET('location'));
var address = $_GET('location');
address=address+',Delhi,India';
alert(address);
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latitude=results[0].geometry.location.lat();
longituderesults[0].geometry.location.lng();
alert("Latitude: "+results[0].geometry.location.lat()+latitude);
alert("Longitude: "+results[0].geometry.location.lng()+longitude);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
});

function $_GET(param) {
var vars = {};
window.location.href.replace(
/[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
function( m, key, value ) { // callback
vars[key] = value !== undefined ? value : '';
}
);
if (param) {
return vars[param] ? vars[param] : null;
}
return vars;
}
</script>

最佳答案

必须使用 AJAX 才能达到您想要的结果。由于需要执行的操作顺序,我看不到其他方法。

这里有一些示例代码,您还需要将代码拆分为两个文件。

file1.php(使用 jQuery AJAX,希望没问题)

<?php
if(isset($_GET)){
$location = $_GET['l'];
}
?>

<div id="test"></div>

<script>
$(function() {

geocoder = new google.maps.Geocoder();

alert('<?=$location?>');
var address = '<?=$location?>';
address=address+',Delhi,India';
alert(address);
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {

var latitude=results[0].geometry.location.lat();
var longitude=results[0].geometry.location.lng();

alert("Latitude: "+results[0].geometry.location.lat()+latitude);//Both of these would be the same?
alert("Longitude: "+results[0].geometry.location.lng()+longitude);
$.ajax (
{
type : "POST",
url : "file2.php",
data :
{
longitude:longitude,
latitude:latitude
},
success : function(result)
{
$('#test').html(result);
},
error: function (xhr, ajaxOptions, thrownError)
{
alert(xhr.status);
alert(thrownError);
}
});

}
else {
alert("Geocode was not successful for the following reason: " + status);
}
});

});
</script>

请注意,您缺少 = 经度变量声明。

file2.php(这是ajax将请求然后拉取数据的文件)

<?php
//connection to your DB here
$user_latitude = $_POST['latitude'];
$user_longitude = $_POST['longitude'];

$result=$con->query("SELECT *, ( 6371 * acos( cos( radians($user_latitude) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians($user_longitude) ) + sin( radians($user_latitude) ) * sin( radians( lat ) ) ) ) AS distance FROM Location HAVING distance < 100 ORDER BY distance LIMIT 0 , 10");

for($x=0;$x<$result->num_rows;$x++){
$row=$result->fetch_assoc();
echo "<div class='result-data'>";
echo $row["name"];
echo "</div>";
}

这样,位置名称将从 URL(类似于 mysite.com/file1.php?l=delhi)加载 $_GET 并传递给 JS,然后对请求数据的 php 文件进行 AJAX 调用数据库。最后显示在 id=test div 中。

这应该可行,但您应该注意 $_GET 存在一些安全问题。 http://www.ultramegasoft.com/blog/2009/08/5-basic-php-security-tips/还有大量关于 $_GET 的信息。如果没有一些紧缩,我不会在生产中使用上面的代码。这种方法还将 JS 链接到 PHP,这并不理想。

关于javascript - 基于 Javascript 函数值,通过 PHP 检索数据库数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34380320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com