Php 将HTML5位置插入MySQL数据库

Php 将HTML5位置插入MySQL数据库,php,mysql,sql,html,location,Php,Mysql,Sql,Html,Location,我想将用户的HTML5位置插入数据库。因此,基本上我希望它提交用户按下submit时所在的位置 HTML5位置代码: var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { x.inn

我想将用户的HTML5位置插入数据库。因此,基本上我希望它提交用户按下submit时所在的位置

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;  
}

为此,您需要一个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......
?>


我没有看到任何代码可以将该位置数据实际发送到服务器。。。您需要处理其他表单字段,所以我不明白为什么您不能处理地理位置数据……所以您的问题基本上是:如何将js传递到php/sql?您可能正在寻找AJAX将javascript数据传递到php
<?php

 $location = $_POST['location'];
 //Then perform your query here......
?>