【问题标题】:GeoLocation track & insert into mysqlDatabse地理位置跟踪并插入 mysqlDatabse
【发布时间】:2020-06-03 12:20:36
【问题描述】:

我想跟踪用户位置并保存到数据库中
我正在使用地理定位脚本在加载页面时将用户位置数据跟踪到 mysql 数据库中。但是地理位置数据没有插入数据库。这是代码

<?php
require('config.php');
$data1=$_POST['location'];
mysqli_query($con,"insert into table(raw) values('$data1')");
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body onload="getLocation()">
<form method="post" action="">
<input name="location" id="demo" type="hidden" /></form>
<script>
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) {
  var latlon = position.coords.latitude + "," + position.coords.longitude;

  var url = "https://www.google.com/maps/@"+latlon;
 x.innerHTML = url;
}
</script>
</body>
</html>

【问题讨论】:

    标签: javascript php geolocation


    【解决方案1】:

    你做错了。您会看到代码执行顺序,正文将在表单之前加载,并且&lt;form&gt; &lt;/form&gt; 没有向 php 脚本发布任何内容,因为只有字段,即隐藏,不包含任何值。 为此,您需要在代码中执行以下操作:

    <pre><form><input type="hidden" id="demo" name="location" value="" />
    <script type="text/javascript">
     var elem = document.getElementById("demo");
     elem.value = navigator.geolocation.getCurrentPosition(showPosition);
    </script></form>
    

    【讨论】:

      猜你喜欢
      • 2020-02-10
      • 2019-11-29
      • 1970-01-01
      • 2017-12-06
      • 2015-01-27
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多