我试图创建一个页面,您点击一个按钮,你得到你的坐标,然后数据保存在服务器上的.txt文件。这是我的代码如何写入从PHP文件
<!DOCTYPE html>
<html>
<head>
<head>
<body>
<input type="hidden" id="Latitude" name="Lat" value="">
<input type="hidden" id="Longitude" name="Lon" value="">
<button onclick="getLocation()">Get Location</button>
<p id="demo"></p>
<script>
var x = document.getElementById("demo");
var lat,lon;
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
lat=position.coords.latitude;
lon=position.coords.longitude;
document.getElementById("Latitude").value = lat;
document.getElementById("Longitude").value = lon;
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
<?php
$marker = fopen("markers.txt",a)
$locLat = $_GET['Lat'];
$locLon = $_GET['Lon'];
fwrite($marker,$locLat);
fwrite($marker,$locLon);
?>
</body>
</html>
我可以得到用户的位置,但PHP代码不起作用。 感谢
fopen(“markers.txt”,'a'),a应该在括号内 – CoreMeltdown
您需要对服务器进行ajax调用或添加表单标签,并让用户手动提交表单请注意,所显示的php已经在您看到该页面时运行并完成。 – jeroen
只需确保您开发出错报告,“a”显然是编译错误。 – Patrick