Putting AJAX in a Joomla Module

久未见 提交于 2019-12-05 13:16:34

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.

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. If there are no variables in the POST or URL it will simply display the module the way Joomla expects.
  2. If the variable are detected call the method to add it to the database and the javascript function that will add it the the display. Also prevent the normal display from happening.

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.

Fron Joomla 3.2 if you needed to make an AJAX request in Joomla! from a custom module or plugin, you can build the URL as below.

index.php?option=com_ajax&module=your_module_name&method=YourMethodName&format=json

Explanation of each parameter

1. index.php?option=com_ajax : ALL requests must be routed through com_ajax.

2. module=your_module_name: The first part of this is the type of extension you're using. This can either be 'module' or 'plugin'. The second part is the name of the extension. please take care you're not including the prefix 'mod_' or 'plg_', just the name.

3. method=YourMethodName: This is the method name that you are trying to call. If you're using this in a module then the method must be appended with 'Ajax'. So, the method represented here will be YourMethodNameAjax. If you're using this on a plugin, the method must be prepended with 'onAjax'. So, the method represented here will be onAjaxYourMethodName.

Check more at Joomla doc

Valentin Despa

From my knowledge you can't do AJAX calls from the module to a function in the module itself (or similar).

You can build a component and create a view which returns RAW or JSON as a response. Because Joomla! itself will be called you can use the Joomla! API & all the goodies from it. It's also a security thing, because if you need to get sensitive data, you can also do ACL / user checks.

When you can call from the module something like:

index.php?option=com_mycomponent&task=getJson

The article Generating JSON output is a good starting point.

subhash
jQuery(document).ready(function () { 
    jQuery(".btnshow").click(function () {
        var pagename, pid;
        pid=jQuery(this).attr('rel');
        pagename="<?php echo JURI::root();?>index.php?option=com_componentname&view=result&Id="+pid;    
        jQuery.get(pagename, function (data) {
            jQuery('.para1').html(data);
        });
    }); 
});


<a href="JavaScript:void(0);" rel="<?php echo $id; ?>" title="<?php echo $id; ?>"><img src="<?php echo $image; ?>" alt="" /></a>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!