gpt4 book ai didi

javascript - 将 php 变量传递给 joomla 中的 javascript

转载 作者:行者123 更新时间:2023-11-30 12:30:36 25 4
gpt4 key购买 nike

我正在尝试为 joomla 制作一个使用 google maps API 制作 map 的模块。

如果数据硬编码在 javascript 文件中, map 本身工作正常,但我希望能够在多个站点上轻松使用它,因此最好从 joomla 后端设置所有选项。我一直在尝试通过 JSON 调用来执行此操作,但它在页面加载时返回错误 500。我的开发网站是: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 文件的其余部分,因为它与这个问题无关,而且它适用于硬编码值。

最佳答案

500 error是因为您正在尝试访问 getMapOptionsAjax() 中的 undefined variable 。

我相信您正在以错误的方式思考这个问题。

如果您只想设置通过 Joomla 的模块管理器在后端定义的值,那么这些值实际上是静态值(即您的 $params)。所以你应该在你的 module 模板文件中设置它们,即 /modules/mod_google_maps/tmpl/default.php

在该文件中,您可以添加一个部分来设置一些 Javascript 变量以供您的 maps.js 文件使用。例如

<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>

它将像这样呈现并发送到浏览器:

<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>

注意:mapDisableUi 将返回它在 mod_google_maps.xml 中分配的值

注意:在 echo 两边为 markerTitle 的字符串值添加单引号 '

关于javascript - 将 php 变量传递给 joomla 中的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com