简体   繁体   English

使用Cookie将GeoLocation从JavaScript发布到PHP

[英]Posting GeoLocation From JavaScript to PHP using Cookies

I am trying to send GoeLocation coordinates from JavaScript to PHP using Cookies and I am getting a Notice as Notice: Undefined index: data in /Applications/XAMPP/xamppfiles/htdocs/samepage.php on line 24 我正在尝试使用Cookie将GoeLocation坐标从JavaScript发送到PHP到PHP,并且得到一个通知,即Notice: Undefined index: data in /Applications/XAMPP/xamppfiles/htdocs/samepage.php on line 24

My file name is samepage.php and I want to post this on the same page. 我的文件名为samepage.php,我想将此文件发布在同一页面上。

My code: 我的代码:

<html>
<head>
<title>Test Geo Location</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}
function showPosition(position) {
    document.getElementById("getlat").value = position.coords.latitude;
    document.getElementById("getlon").value = position.coords.longitude;
}
$( document ).ready(function() {
$.cookie("data",{getlat:$("#getlat").val(),getlon:$("#getlon").val()});
});
</script>
</head>
<body onload="getLocation();">
<input type="text" id="getlat" name="getlat" value="<?php echo $_POST['polat']; ?>" /> 
<input type="text" id="getlon" name="getlon" value="<?php echo $_POST['polon']; ?>" />
<?php
$data = json_decode($_COOKIE["data"]);
$lat = $data["getlat"];
$lon = $data["getlon"];
?>
</body>
</html>

There are a few things you need to make sure: 1) php code runs in server side; 您需要确保以下几点:1)php代码在服务器端运行; 2) javascript code runs in client side; 2)JavaScript代码在客户端运行; 3) cookie operation is not supported by jQuery by default. 3)默认情况下,jQuery不支持cookie操作。 please see relevant question 请参阅相关问题

4) in php json_decode function, a stdClass object will be returned, not an array. 4)在php json_decode函数中,将返回一个stdClass对象,而不是数组。 Please see json_decode 请参阅json_decode

What you need to do is: 1) include cookie jquery plugin 2) set cookie by js function first and then call php URL 您需要做的是:1)包括cookie jquery插件2)首先通过js函数设置cookie,然后调用php URL

I share a revised version with you and you will get what you expected(You need to download jquery.cookie.js first). 我与您共享一个修订版,您将获得期望的结果(您需要先下载jquery.cookie.js)。

<html>
<head>
<title>Test Geo Location</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="jquery.cookie.js"></script>

<script>
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
}
function showPosition(position) {
    document.getElementById("getlat").value = position.coords.latitude;
    document.getElementById("getlon").value = position.coords.longitude;
}

function doSubmit(){
    $.cookie("data",'{\"getlat\":' + $("#getlat").val() + ',\"getlon\":' + $("#getlon").val() + '}');
    document.form1.submit();
}
$( document ).ready(function() {


});
</script>
</head>
<body onload="getLocation();">
<form name="form1">
<input type="text" id="getlat" name="getlat" value="<?php echo $_POST['polat']; ?>" /> 
<input type="text" id="getlon" name="getlon" value="<?php echo $_POST['polon']; ?>" />
<input type="button" value="submit" onclick = "doSubmit()">
</form>
<?php
$data = json_decode($_COOKIE["data"]);
$lat = $data->getlat;
$lon = $data->getlon;
?>
</body>
</html>

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

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