doucuo8618 2017-01-07 08:06
浏览 91
已采纳

在按钮点击时将twig中的javascript变量传递给php控制器

I have Google maps and a rectangle highlighting some area on top of the map. NorthEast and Southwest latitudes of that rectangle are stored as javascript variables(ne,sw).I want to send those parameters to a php controller on button click, like this:

View

How can I do that? I d'ont have knowledge of AJAX nor JQuery.

{% extends 'base.html.twig' %}

{% block body %}

var ne;
var sw;

<a href="/highlighted/{{something}}" class="btn btn-success">View</a>
<div id="map" style="width:100%;height:500px"></div>


<script language="javascript" type="text/javascript">
function myMap() {
map = new google.maps.Map(document.getElementById('map'), {          center: {lat: 6.911918, lng: 79.892780},
    zoom: 9
  });

  var bounds = {
    north: 7.011918,
    south: 6.811918,
    east:  79.992780,
    west:  79.792780
  };

  // Define the rectangle and set its editable property to true.
  rectangle = new google.maps.Rectangle({
    bounds: bounds,
    editable: true,
    draggable: true
  });

  rectangle.setMap(map);
  // Add an event listener on the rectangle.
  rectangle.addListener('bounds_changed', showNewRect);

  // Define an info window on the map.
  infoWindow = new google.maps.InfoWindow();
}

// Show the new coordinates for the rectangle in an info window.

/** @this {google.maps.Rectangle} */
function showNewRect(event) {
  var ne = rectangle.getBounds().getNorthEast();
  var sw = rectangle.getBounds().getSouthWest();

  map.northLatitude=2.33;

  var contentString = '<b>Rectangle moved.</b><br>' +
      'New north-east corner: ' + ne.lat() + ', ' + ne.lng() + '<br>' +
      'New south-west corner: ' + sw.lat() + ', ' + sw.lng();

  // Set the info window's content and position.
  infoWindow.setContent(contentString);
  infoWindow.setPosition(ne);

  infoWindow.open(map);

    //window.location.href = "Controller/SatelliteImages_Controller.php?w1=" + ne + "&w2=" + sw;
    return (ne+sw);
}

</script>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyALi1Ro_johRtFp2JrLa9zGMLTwBMxBrHQ&callback=myMap"></script>

{% endblock %}

Please excuse my bad syntax. I'm new to PHP and Symfony.

  • 写回答

1条回答 默认 最新

  • duanbi6522 2017-01-07 10:35
    关注

    Assuming that your code is working you can do this easily with jquery:

    <script language="javascript" type="text/javascript">
      $( document ).ready(function() {
        // Your script code here
    
        function showNewRect(event) {
          // Your code function here
    
          var neSw = {"data": {"ne": ne, "sw": sw}};
          $.ajax({
             url: 'Controller/SatelliteImages_Controller.php',
             type: "post",
             data: neSw
          })
          .done(function(responseData, status){
            console.log(responseData);
          })
          .fail(function(object, status){
            console.log("Error in Ajax response");
          });
        }  
      });          
    </script>
    

    So you send your vars ne sw by POST to your controller. In your controller you can get it by your request object: $request->get('data');

    If you want you can send back a response data to the AJAX function to inform all was ok, using Symfony\Component\HttpFoundation\JsonResponse;

    In your Symfony controller:

    use Symfony\Component\HttpFoundation\JsonResponse;
    
    class YourController extends Controller
    {
        public function yourAction() {
            $data = $this->getRequest()->get('data');
            var_dump($data); // Here you can see your vars sent by AJAX
            $dataResponse = array("error" => false); //Here data you can send back
            return new JsonResponse($dataResponse);
        }
    }
    

    If you do this you must to configure your jquery AJAX function to receive json data:

        $.ajax({
           url: 'Controller/SatelliteImages_Controller.php',
           type: "post",
           data: neSw,
           dataType: 'json'
        })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能