Put AJAX in Joomla module

I created the base Joomla module for my site as a scream box. But I would like to put AJAX in it (I know that a similar module with AJAX already exists on JED, but this is another project for me to find out how AJAX works in the Joomla module).

The usual AJAX stuff in which you redirect a new php file obviously does not work as the file will not be defined as

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

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

The way of another cry-boxing module I saw this to point to a function in the helper.php file. Which makes sense to me, since that is where all the functions should be stored. However, it is unclear how the module accessed the helper.php file in the onSubmit () command (or related), and hoped that someone could shed some light on this.

I really don't need something specific for my shoutbox module - it's more a question of how to get AJAX functionality in Joomla modules and how it is arranged

+4
source share
4 answers

The other two were on the right track, you need to remember that when you make your AJAX call, you cannot directly access the php file in Joomla. Therefore, it is better to call your module instead. In this case, check the module for variables in POST or in the URL.

I am a big fan of jQuery ajax, it is much more autonomous than the method used by the one who built this shoutbox.

 $( "#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 commentary on Valentine, you:

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

Call only the necessary functions when variables exist.

To explain part of the process, it looks something like this:

  • If there are no variables in the POST or URL, it will simply display the module as Joomla expects.
  • If a variable is found, call the method to add it to the database and a javascript function that will add it to the display. Also prevent a normal display.

The module you referenced was quite interesting. Helper functions that reference the main file do what the model would normally handle in MVC, and Javascript also looked like everything. If you really want to understand how this works, you really need to insert the fatAjax.js file as it contains all AJAX and sets the variables that mod_shoutbox.php listens for.

+5
source

From my knowledge, you cannot make AJAX calls from module to function in the module itself (or similar).

You can create a component and create a view that returns RAW or JSON as an answer. Because Joomla! itself will be called, you can use Joomla! API and all the benefits of it. This is also a security thing, because if you need to get sensitive data, you can also perform ACL / user checks.

When you can call something like:

index.php?option=com_mycomponent&task=getJson

The article Generating JSON Output is a good starting point.

+1
source
 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> 
0
source

Fron Joomla 3.2 if you need to make an AJAX request in Joomla! from a user module or plugin you can create a url as shown 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 should be redirected through com_ajax.

2. module = your_module_name . The first part of this type is the type of extension used. It can be either a โ€œmoduleโ€ or a โ€œpluginโ€. The second part is the name of the extension. be careful, you do not include the prefix 'mod_' or 'plg_', just a name.

3. method = YourMethodName . This is the name of the method you are trying to call. If you use this in a module, then the method must be added using "Ajax". So the method presented here will be YourMethodNameAjax . If you use this in a plugin, the method must be added using 'onAjax'. So the method presented here will be onAjaxYourMethodName .

Read more at Joomla doc

0
source

Source: https://habr.com/ru/post/1446840/


All Articles