简体   繁体   English

通过cookie将javascript变量传递到另一个页面并使用php进行检索

[英]Pass javascript variable via cookie to another page and retrieve using php

I am using Google Maps API v3 to set a marker and I am having trouble submitting the marker coordinates to a MYSQL database via PHP. 我正在使用Google Maps API v3设置标记,但无法通过PHP将标记坐标提交给MYSQL数据库。 I am trying to pass the javascript value for "event.latlng" to the submission page via a cookie. 我正在尝试通过Cookie将“ event.latlng”的javascript值传递到提交页面。 Since PHP is loaded server-side first, I can't use PHP to pass the variable via URL to the submission page. 由于PHP首先在服务器端加载,因此我无法使用PHP通过URL将变量传递到提交页面。 So I am passing the static variables via PHP to the submission page. 因此,我正在通过PHP将静态变量传递到提交页面。 I can't get the cookie part working though. 我无法使Cookie部分正常工作。 I thought that with a javascript function dedicated to retrieving the current coordinates of the marker and placing them in a cookie, I could call that function with onclick in a href button submission link. 我认为使用专门用于检索标记的当前坐标并将其放置在cookie中的javascript函数,我可以在href按钮提交链接中使用onclick调用该函数。 No luck so far. 到目前为止没有运气。 "mapNew4.php" is the page I have the map on, "saveLocation.php" is the submission (to the database via PHP) page. “ mapNew4.php”是我在上面的地图页面,“ saveLocation.php”是提交(通过PHP到数据库)页面。 This is the important parts of the "mapNew4.php" page. 这是“ mapNew4.php”页面的重要部分。 I haven't tried to retrieve the cookie yet on the "saveLocation.php" page because this code already causes the "mapNew4.php" page to stop loading. 我尚未尝试在“ saveLocation.php”页面上检索cookie,因为此代码已经导致“ mapNew4.php”页面停止加载。 Everything else works fine without the addition of the javascript function and onclick call. 在不添加javascript函数和onclick调用的情况下,其他所有工作都很好。

<script type="text/javascript">

function myLatLng()
{
var myLatLng = event.latLng;
setcookie('mapNew4['myLatLng']', time()+(10), '/');
return false;
};

</script>

<?php
<li>
<a href="saveLocation.php/?meetID='. $meetingID .'&courseID='. $courseID .'&groupID='.        $groupID .'" onclick="javascript:myLatLng();"  data-icon="check" data-transition="flip"      style="background: green;">Save</a>
</li>
?>

Since myLatLng() always returns false and it is called via onclick , the target of the link (specified in the href attribute) never gets called. 由于myLatLng()始终返回false并通过onclick调用,因此永远不会调用链接的目标(在href属性中指定)。 There may be other problems, but certainly you mean for saveLocation.php to get called when the link is clicked. 可能还有其他问题,但是肯定您的意思是单击链接时调用saveLocation.php

Your PHP code seems to be wrong either. 您的PHP代码似乎也不对。 You are declaring HTML within the PHP block. 您在PHP块中声明HTML。 Is this site even loading? 该网站是否正在加载? Your JS callback function uses an event variable. 您的JS回调函数使用事件变量。 Where does it come from. 它从何而来。 Some browsers would use the click event of your link here when event has not been declared elsewhere. 如果未在其他地方声明事件,则某些浏览器会在此处使用链接的click事件。

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

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