繁体   English   中英

在单击时将树枝中的javascript变量传递给php控制器

[英]Pass javascript variable in twig to php controller on button click

我有Google地图和一个突出显示地图顶部某些区域的矩形。 该矩形的东北和西南纬度存储为javascript变量(ne,sw)。我想将这些参数发送到单击按钮时的php控制器中,例如:

视图

我怎样才能做到这一点? 我没有AJAX或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 %}

请原谅我的语法错误。 我是PHP和Symfony的新手。

假设您的代码正常工作,则可以使用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>

所以,你把你的增值经销商ne sw邮寄到您的控制器。 在您的控制器中,您可以通过请求对象获得它: $request->get('data');

如果需要,可以使用Symfony\\Component\\HttpFoundation\\JsonResponse;将响应数据发送回AJAX函数,以告知一切正常Symfony\\Component\\HttpFoundation\\JsonResponse;

在您的Symfony控制器中:

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);
    }
}

如果这样做,则必须配置jquery AJAX函数以接收json数据:

    $.ajax({
       url: 'Controller/SatelliteImages_Controller.php',
       type: "post",
       data: neSw,
       dataType: 'json'
    })

暂无
暂无

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

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