【问题标题】:Insert HTML5 location into MySQL database将 HTML5 位置插入 MySQL 数据库
【发布时间】:2014-12-04 20:03:05
【问题描述】:

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

【问题讨论】:

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

标签: php mysql sql html location


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 2023-04-10
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2012-06-01
    相关资源
    最近更新 更多