将HTML5位置插入MySQL数据库

时间:2014-12-04 20:03:06

标签: php mysql sql html5 location

我想将用户的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."')

1 个答案:

答案 0 :(得分:2)

您将需要一个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......
?>