2015-01-08 2 views
0

Google Maps API를 사용하여지도를 만드는 joomla 용 모듈을 만들려고합니다.joomla에서 javascript로 PHP 변수 전달하기

데이터가 javascript 파일에 하드 코드 된 경우 맵 자체가 제대로 작동하지만 여러 사이트에서 쉽게 사용할 수 있기를 원하므로 joomla 백엔드에서 모든 옵션을 설정하는 것이 좋습니다. JSON 호출로이 작업을 시도했지만 페이지가로드 될 때 오류 500을 반환합니다. 내 dev에 사이트는 다음과 같이 http://dev.xander.dk.web1.symatic.dk/

관련 코드는 다음과 같습니다

mod_google_maps.php (변수를 만들고 mod_google_maps.xml의 필드에 연결하기)

<?php 
/** 

* Hello World! Module Entry Point 
* 
* @package Joomla.Tutorials 
* @subpackage Modules 
* @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module 
* @license  GNU/GPL, see LICENSE.php 
* mod_helloworld is free software. This version may have been modified pursuant 
* to the GNU General Public License, and as distributed it includes or 
* is derivative of works licensed under the GNU General Public License or 
* other free or open source software licenses. 
*/ 

// no direct access 
defined('_JEXEC') or die; 
// Include the syndicate functions only once 
require_once(dirname(__FILE__) . '/helper.php'); 

$mapCenterLat = $params->get('mapCenterLat', '55.395239'); 
$mapCenterLng = $params->get('mapCenterLng', '10.380180'); 
$zoom = $params->get('zoom', '18'); 
$mapDisableUi = $params->get('mapDisableUi', false); 
$markerLat = $params->get('markerLat', '55.395239'); 
$markerLng = $params->get('markerLng', '10.380180'); 
$markerTitle = $params->get('markerTitle', 'symatic'); 

helper.php

<?php 
function getMapOptionsAjax(){ 
    $data = array('mapCenterLat' => $mapCenterLat, 'mapCenterLng' => $mapCenterLng, 'zoom' => $zoom, 'mapDisableUi' => $mapDisableUi, 'markerLat' => $markerLat, 'markerLng' => $markerLng, 'markerTitle' => $markerTitle); 
    echo json_encode($data); 
}; 
?> 

maps.js (단지 관련 JSON 호출)

jQuery.get('index.php?option=com_ajax&module=google_maps&method=getMapOptions&format=json', function(data){ 
    var mapResponse = data; 
    console.log(mapResponse); 
}); 

이 질문과 관련이 없으며 하드 코드 된 값으로 작동하기 때문에 나머지 maps.js 파일을 생략했습니다.

답변

2

500 error은 사용자가 getMapOptionsAjax()에서 정의되지 않은 변수에 액세스하려고하기 때문입니다.

나는이 잘못된 생각을 주위에서 생각하고 있다고 생각합니다.

Joomla의 모듈 관리자를 통해 백엔드에서 정의 된 값만 설정하려는 경우 실제로는 정적 값입니다 (즉 $params). 따라서 module 템플릿 파일에 설정해야합니다. 즉, /modules/mod_google_maps/tmpl/default.php

파일에 maps.js 파일에서 사용할 자바 스크립트 변수를 설정하는 섹션을 추가 할 수 있습니다. 예 :

<script> 
    var mapCenterLat = 55.395239; 
    var mapCenterLng = 10.380180; 
    var zoom = 18; 
    var mapDisableUi = false; 
    var markerLat = 55.395239; 
    var markerLng = 10.380180; 
    var markerTitle = 'symatic'; // Note the quotes added around the echo 
</script> 

: 렌더링과 같은 무언가로 브라우저에 전송

<script> 
    var mapCenterLat = <?php echo $params->get('mapCenterLat', '55.395239'); ?>; 
    var mapCenterLng = <?php echo $params->get('mapCenterLng', '10.380180'); ?>; 
    var zoom = <?php echo $params->get('zoom', '18'); ?>; 
    var mapDisableUi = <?php echo $params->get('mapDisableUi', false); ?>; 
    var markerLat = <?php echo $params->get('markerLat', '55.395239'); ?>; 
    var markerLng = <?php echo $params->get('markerLng', '10.380180'); ?>; 
    var markerTitle = '<?php echo $params->get('markerTitle', 'symatic'); ?>'; 
</script> 

이 mod_google_maps.xml에 할당 된 mapDisableUi가 값을 반환

참고 : 작은 따옴표를 'echo 주변에 추가했습니다. 문자열 값이 markerTitle 인 경우

+0

꽤 완벽했습니다. 두 가지 생각 : 먼저 스크립트의 모든 줄 끝에는 세미콜론이 필요합니다. 그렇지 않으면 작동하지 않습니다. 둘째, mapDisableUi는 true/false를 반환해야합니다. 즉, xml 파일에 할당 한 두 값입니다. 그러나 이것은 매우 유용했습니다. 괜찮 으면 나중에 참조 할 수 있도록이 작업과 같은 AJAX 호출 방법을 설명해 주시겠습니까? – xanderh

+1

세미콜론에 @xanderh mybad하지만 방금 브라우저에 입력되었으므로 AJAX 호출과 Joomla는 [Joomla Q & A StackExchange 사이트에서이 응답을 보았습니다.] (http : //joomla.stackexchange.com/a/147/107). – Craig