【问题标题】:cakephp: access hidden field value set in ctp, in controller without form postcakephp:访问在 ctp 中设置的隐藏字段值,在没有表单发布的控制器中
【发布时间】:2013-02-19 18:39:54
【问题描述】:

我正在使用 cakephp 和 javascript。以下是场景: 点击地图图标,会调用一个显示谷歌地图的 jquery 函数。现在通过javascript,用户可以拖动n改变谷歌地图中的当前标记位置。这些值也在视图中设置。但是我如何在控制器中访问这些值(纬度,经度)? 我希望在会话变量中设置纬度和经度,但我不能这样做,我的意思是 icant 在 javascript 中设置会话变量 n 在控制器中使用它。 以下是我的代码:

    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

    }

我该怎么做?

【问题讨论】:

    标签: javascript session cakephp


    【解决方案1】:

    可以通过 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,但它使您能够调用操作而无需定义视图。欲了解更多信息,您可以查看the documentation.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      相关资源
      最近更新 更多