Javascript cakephp:访问ctp中设置的隐藏字段值,在控制器中不使用表单post
我在cakephp和javascript中工作。以下是场景: 单击地图图标时,会调用一个jquery函数,它显示google地图。现在通过javascript,用户可以拖动并更改google地图中当前标记的位置。这些值也在视图中设置。但如何在控制器中访问这些值(纬度、经度)? 我希望在会话变量中设置纬度和经度,但我不能这样做,我的意思是我不能在javascript中设置会话变量,也不能在控制器中使用它。 以下是我的代码:Javascript cakephp:访问ctp中设置的隐藏字段值,在控制器中不使用表单post,javascript,session,cakephp,Javascript,Session,Cakephp,我在cakephp和javascript中工作。以下是场景: 单击地图图标时,会调用一个jquery函数,它显示google地图。现在通过javascript,用户可以拖动并更改google地图中当前标记的位置。这些值也在视图中设置。但如何在控制器中访问这些值(纬度、经度)? 我希望在会话变量中设置纬度和经度,但我不能这样做,我的意思是我不能在javascript中设置会话变量,也不能在控制器中使用它。 以下是我的代码: function initialize() { var la
function initialize() {
var latLng = new google.maps.LatLng(lat,long);
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom:8,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});
// Update current position info.
//alert(" initilize");
updateMarkerPosition(latLng);
geocodePosition(latLng);
// Add dragging event listeners.
google.maps.event.addListener(marker, 'dragstart', function() {
//updateMarkerAddress('Dragging...');
// alert("dragStart"+marker.getPosition()); // access this value in controller
});
google.maps.event.addListener(marker, 'drag', function() {
// updateMarkerStatus('Dragging...');
updateMarkerPosition(marker.getPosition());
alert(marker.getPosition()); // access this value in controller
});
google.maps.event.addListener(marker, 'dragend', function() {
// updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
alert(marker.getPosition()); // access this value in controller
});
}
在控制器中:
public function map()
{
//access lat, long here
}
如何做到这一点?可以使用AJAX技术将JavaScript与PHP连接起来 像这样,JavaScript代码将在控制器中调用一个具体的操作,一旦进入控制器,您就可以将其保存在会话中,或者使用它执行任何操作 您可以每X秒或根据任何更改调用一次该操作,这取决于您希望调用它的时间以及您可以从用户捕获的可能事件 这是我的意思的一个例子:
$(document).ready(function(){
//calling the function every 30 seconds
setInterval(function(){
getUserSavings();
}, 30*1000);
});
function getUserSavings(){
$.post("http://"+ document.domain + "/users/getSavings.json",
function(dat){
//do whatever you want with the resulting data (dat)
$('#totalSavings span').html(dat['result']['savings']);
});
}
不需要使用扩展名json
,但它使您无需定义视图即可调用操作。有关更多信息,请查看