我有以下代码:
<script type="text/javascript" src="../scripts/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
var Location_lat;
var Location_lng;
function showMap(position) {
Location_lat=position.coords.latitude;
alert(Location_lat);
}
navigator.geolocation.getCurrentPosition(showMap);
<?php
$Location_lat="<script>document.write(Location_lat)</script>"?>
</script>
<?php echo 'php_'.$Location_lat;?>
执行时,警报返回正常值但$ Location_lat未定义。有帮助吗?感谢
答案 0 :(得分:1)
如果您不需要服务器上坐标的值,那么只需使用jquery或简单的javascript显示它。例如,您可以将代码更改为
<script type="text/javascript" src="../scripts/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
var Location_lat;
var Location_lng;
function showMap(position) {
Location_lat=position.coords.latitude;
alert(Location_lat);
$(body).append(Location_lat);
}
navigator.geolocation.getCurrentPosition(showMap);
</script>
请避免将服务器端脚本(php)与客户端脚本(javascript)混合使用。为此目的,学习AJAX