2014-12-04 122 views
0

我想将用户的HTML5位置插入到数据库中。所以基本上我希望它提交用户在他/她按下提交时的位置。将HTML5位置插入MySQL数据库

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."') 
+0

我没有看到任何代码实际发送该位置数据到服务器......你有东西来处理其他表单域,所以我不明白为什么你不能处理地理定位数据... – 2014-12-04 20:04:36

+0

所以你的问题基本上是:如何将js传递给php/sql? – webeno 2014-12-04 20:05:42

+0

您可能正在寻找AJAX将JavaScript数据传输到PHP – Jordy 2014-12-04 20:08:15

回答

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...... 
?>