在一个Joomla模块把AJAX [英] Putting AJAX in a Joomla Module

查看:243
本文介绍了在一个Joomla模块把AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的网站是一个在线留言板做了一个基本的Joomla模块。但我喜欢把AJAX它(我知道类似的模块,AJAX已经存在于JED但是这更多的是项目对我来说,学习AJAX是如何工作的一个Joomla模块)。

I've made a basic Joomla module for my site as a shoutbox. But I'd like to put AJAX in it (I know a similar module with AJAX already exists on JED but this more a project for me to learn how AJAX works in a Joomla module).

通常的AJAX的东西,你重定向到一个新的PHP文件显然不能作为文件不会被定义为

The usual AJAX stuff where you redirect to a new php file obviously doesn't work as the file will not be defined as

   defined('_JEXEC') or die('Restricted access');

将在一个新的页面失败。并确定_JEXEC等于一(因为我读过的几个职位的SO)据我读过有关的Joomla Docs是一个安全隐患,因为它提供了一个入口点到网站。

will fail in a new page. And defining _JEXEC to be equal to one (as I've read in several posts on SO) as far as I've read on Joomla Docs is a security risk as it provides an entry point onto the site.

我见过href="http://extensions.joomla.org/extensions/communication/shoutbox/43" rel="nofollow">其他在线留言板模块的

The way the other shoutbox module I've seen does is to point back at a function in the helper.php file. Which makes sense to me as that is where all the functions should normally be stored. However I'm unclear as to how to the module was accessing the helper.php file on a onSubmit() (or a related) command and was hoping someone could shed some light on this.

我实际上并不需要具体的东西到我的吼吼箱模块 - 这更多的是如何让AJAX功能中的Joomla问题的模块以及它是如何安排

I don't actually need anything specific to my shoutbox module - this is more a question of how to get AJAX functionality in Joomla modules and how it is arranged

推荐答案

另外两个人到正确的轨道,你需要记住,当你让你的AJAX调用,你不能直接访问一个PHP文件中的Joomla。所以,相反,它是更好地拨打你的模块。在这种情况下,具有该模块为您在您的文章,或URL中的变量。

The other two were onto the right track you need to remember that when you make your AJAX call you can't directly access a php file in Joomla. So instead it's better to make calls to your module. In this case and have the module check for variables in your POST or in the URL.

我jQuery的阿贾克斯它含有比方法更大量的自我的忠实粉丝是谁建使用的在线留言板的家伙。

I'm a big fan of JQuery's ajax it's a lot more self contained than the method the guy who built that shoutbox used.

$( "#addShout" ).click( function(event, ui) {
                $.ajax({
                    type: 'GET',
                    url: "<?php echo JURI::base() . "index.php?option=mod_mymodule&task=getCustomerJson&selectedCustomer="?>"  + encodeURIComponent(value),
                    success:function(data){
                        $('#shouts').append(data);
                    },
                    error:function(){
                        $('#errors').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
                    }
                });  
        });

接着,当我在我的评论,以瓦伦丁你所提到的:

Then as I mentioned in my comment to Valentin you:

$task = JRequest::getVar('task'); 
if($task == "getCustomerJson"){mySuperHelper::getCustomerJson();}

您只有在变量存在调用必要的功能。

You only call the necessary functions when the variables exist.

要解释的过程中,它有点像这样的一部分:

To explain part of the process it's kinda like this:

  1. 如果有在POST或URL没有变量它将简单地显示模块的Joomla期望的方式。
  2. 如果变量检测通话将它添加到数据库中的方法和JavaScript函数,将增加它的显示。另外prevent正常显示的发生。

您引用的模块,还是挺有意思的。该辅助函数引用的主文件做什么模型通常会在MVC和Javascript的处理也有点遍。如果你真的想了解一个工作,你真的需要深入到fatAjax.js文件,因为它包含了所有的AJAX,并设置了mod_shoutbox.php侦听的变量。

The module you referenced was quite interesting. The helper functions that the main file referenced does what a model would normally handle in MVC and the Javascript was also kinda all over. If you really want to understand how that one worked you really need to dig into the fatAjax.js file as it contains all of the AJAX and sets the variables that the mod_shoutbox.php listens for.

这篇关于在一个Joomla模块把AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆