简体   繁体   English

将 JavaScript 获取的地理位置传递到 HTML 表单中以发送到数据库

[英]Passing Geo location obtained by JavaScript into a HTML form to be sent to a Database

I'm trying to get the user's current location and have it store in a hidden input so that once they finish filling up the form their location is sent together with everything else to the database to be stored.我正在尝试获取用户的当前位置并将其存储在一个隐藏的输入中,这样一旦他们完成填写表单,他们的位置就会与其他所有内容一起发送到数据库中进行存储。

The problem I'm getting is undefined index on my php, which leads me to believe that the coordinates are not being sent to the input.我遇到的问题是我的 php 上的未定义索引,这让我相信坐标没有被发送到输入。

I have looked up to other posts that talked about the same topic but could not find an answer for my problem.我查阅了其他讨论相同主题的帖子,但找不到我的问题的答案。

Here is my HTML Form (the DIVs there are just to display the coordinates):这是我的 HTML 表单(那里的 DIV 只是为了显示坐标):

<form method="POST" action="example.php">
        <br />
        <div id="latGPS"></div>
        <input type="hidden" id="geoPosLat" value ="" />
        <br />
        <div id="lonGPS"></div>
        <input type="hidden" id="geoPosLon" value ="" />
        <br />
        <input type="text" name="addPoiDescription" placeholder="Description" style="width:200px; height:100px" />
        <br />
        <input type="submit" value="Submit!" />
    </form>

Javascript: Javascript:

function init(){

//Get user's geolocation
if(navigator.geolocation)
{
    navigator.geolocation.watchPosition (processPosition, handleError, {enableHighAccurary:true, maximumAge: 5000});

}
else
{
    alert("Sorry, geolocation not supported");
}

}


// Process GPS Information
function processPosition(gpspos)
{
var latGPS = gpspos.coords.latitude;
    var lonGPS = gpspos.coords.longitude;

//Show their location
document.getElementById("latGPS").innerHTML = 'Latitude: ' + latGPS;
document.getElementById("lonGPS").innerHTML = 'Longitude: ' + lonGPS;

//store location 
document.getElementById("geoPosLat").innerHTML = latGPS;
document.getElementById("geoPosLon").innerHTML = lonGPS;
}

function handleError(err)
{
alert ('An error occurred: ' + err.code);
 }

PHP: PHP:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$aPoiName = $_POST["addPoiName"];
$aPoiCountry = $_POST["addPoiCountry"];
$aPoiRegion = $_POST["addPoiRegion"];
$aPoiType = $_POST["addPoiType"];
$aPoiDescription = $_POST["addPoiDescription"];
$lat = $_POST["geoPosLat"];
$lon = $_POST["geoPosLon"];


// Curl -----------------
// Initialize
$connection = curl_init();

//URL to sned the request to
curl_setopt($connection, CURLOPT_URL, 'https://example.php');

$dataToPost=
[   "Name" => $aPoiName ,
    "Country" => $aPoiCountry ,
    "Region" => $aPoiRegion , 
    "Type" => $aPoiType ,
    "Description" => $aPoiDescription,
    "Lat" => $lat,
    "Lon" => $lon
];
//Execute the request 
curl_setopt($connection,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($connection,CURLOPT_POSTFIELDS,$dataToPost);
$response = curl_exec($connection);

//HTTP CODE
$httpCode = curl_getinfo($connection, CURLINFO_HTTP_CODE);
echo "The script returned the HTTP status code: $httpCode <br />";
//close 
curl_close($connection);

?>

Not sure if this will fix your problem or not however I'm pretty sure you need to specify a name in your HTML for each of your fields and then link them up to your PHP rather than using the ID.不确定这是否能解决您的问题,但我很确定您需要在 HTML 中为每个字段指定一个名称,然后将它们链接到您的 PHP,而不是使用 ID。

For example your HTML would be something like例如,您的 HTML 将类似于

  <input name="LATPOSNAME" type="hidden" id="geoPosLat" value ="" />

and then you would hook that name upto your php like so然后你会像这样把这个名字挂到你的 php 上

$lat = $_POST["LATPOSNAME"];

The element's name attribute name="unique-name-here" value is used by PHP as key to enable access to the data value of the specified form field element when you submit the form.元素的名称属性name="unique-name-here"值被 PHP 用作键,以便在您提交表单时访问指定表单字段元素的数据值。

If you don't declare a name then your PHP will be unable to get your value as there is no key.如果您不声明名称,那么您的 PHP 将无法获取您的值,因为没有键。 This means you can't access that element form data value after the form has been submitted to the server because its key is undefined.这意味着您无法在表单提交到服务器后访问该元素表单数据值,因为其键未定义。

为了使您必须使用 Ajax 和 jquery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM