我想将用户的HTML5位置插入数据库。所以基本上我希望它提交用户按下提交时的位置。
HTML5 位置代码:
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
我当前的代码:
$post = filter_input(INPUT_POST, 'post', FILTER_SANITIZE_SPECIAL_CHARS);
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
INSERT INTO news (post, name, date, location) VALUES ('".$post."', '".$name."', '".date('Y-m-d H:i:s')."', '".$location."')
您将需要一个 Ajax 调用,例如:
function postData(phpFile, newData){
$.ajax({
type: 'POST',
data: newData,
url: phpFile,
success: function(data) {
},
error: function() {}
});
}
当按下提交按钮时,您将调用postData('yoursavephpfile.php', { "location" : getLocation() });通过 onclick 属性。(假设您的 getLocation 返回位置值)注意:不要忘记将最新的jquery添加到您的标头中:
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
您的 php 文件如下所示:
<?php
$location = $_POST['location'];
//Then perform your query here......
?>