2015-04-17 78 views
-1

我试图创建一个页面,您点击一个按钮,你得到你的坐标,然后数据保存在服务器上的.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代码不起作用。 感谢

+1

fopen(“markers.txt”,'a'),a应该在括号内 – CoreMeltdown

+5

您需要对服务器进行ajax调用或添加表单标签,并让用户手动提交表单请注意,所显示的php已经在您看到该页面时运行并完成。 – jeroen

+0

只需确保您开发出错报告,“a”显然是编译错误。 – Patrick

回答

0

你应该把“一”在PHP代码结束第一线双引号和分号

<?php 
$marker = fopen("markers.txt","a"); 
$locLat = $_GET['Lat']; 
$locLon = $_GET['Lon']; 
fwrite($marker,$locLat); 
fwrite($marker,$locLon); 
?> 

见你怎么可以包含表单标签这个问题和Ajax使用提交纠正你的代码php here

+0

看看OP在其他人留下的问题下的意见。你在'a'周围缺少引号,加上即使已经添加了,代码仍然不起作用。 –

+0

感谢@fred -ii指出,我已更正了代码。 –

+1

不客气的Rajnikant。但正如我已经说过的那样;这仍然不会使脚本将坐标写入文件。 [阅读jeroen的评论为什么...](http://stackoverflow.com/questions/29697386/how-to-write-to-file-from-php#comment47530912_29697386) –