如何从PHP写入文件

时间:2015-04-17 11:01:59

标签: javascript php

我试图创建一个页面,您点击一个按钮,您得到您的坐标,然后数据保存在服务器上的.txt文件中。这是我的代码

<!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 个答案:

答案 0 :(得分:0)

您应该通过放置&#39;&#39;来纠正您的代码。在双引号和分号的最后一行PHP代码

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

请参阅此问题,了解如何使用php here

添加表单标记和ajax提交