Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    hyperlocal-marketplace-guide

    Ajax Call from Plugins and Modules In Joomla

    Introduction

    Ajax makes it possible to create websites that are dynamic and fast.
    We can use Ajax to refresh a specific section of a page without reloading all the content or to validate single element without submitting the whole form.

    While developing an extension for Joomla we may encounter a task which requires jQuery Ajax call it may be for the dynamically changing content of an element or to get data based on user input or to perform some operation in the background.

    If we are developing a component then we do not have any issue as we can make Ajax request to any controller of the component but what if we need to create only a plugin or module we might stuck here.

    As in earlier Joomla version, we need to create our own component if we need Ajax call but Joomla! 3.2 includes an AJAX Interface.

    Basically, com_ajax is a component which serves as an interface for Ajax calls for standalone plugins and modules.

    Some Use Cases

    • First, A module/plugin that retrieves data from an external API
    • Second, A module/plugin that interacts with a component that you did not develop
    • Third, A module/plugin that requires data using Ajax, we can write ajax method in same module/plugin

    How does Ajax Call work for modules and plugins?

    First of all, when making Ajax call from module/plugin URL must be, index.php?option=com_ajax.

    Other Parameters:

    Required –

    option=com_ajax

    [module|plugin]=name

    format=[json|debug|raw]

    Optional –

    method=[method name] defaults is get if not provided.

    A request to ?option=com_ajax&module=search would call mod_search with results returned in the default format. Similarly ?option=com_ajax&plugin=search&format=json would trigger the onAjaxSearch with results returned in JSON.

    com_ajax for Modules

    For using Ajax call in Modules some requirements are:

    1. A method must be defined in helper.php
    2. The request must include a module variable in the URL.Example:module=search for mod_search
    3. Optionally method variable can be used in the URL to replace default get method. Example: method=myFunction this will trigger myFunctionAjax

    Example Request


    Above code will trigger method getDataAjax in helper.php of search module.

    com_ajax for Plugins

    For using Ajax call in plugins some requirements are:

    1. The method name must start with onAjax. Example: onAjax[Name]
    2. The request must include a plugin variable in the URL.Example:plugin=getData this will trigger onAjaxgetData
    3. Optionally group variable can be used in the URL to specify plugin group.

    Example Request


    Above code will trigger method onAjaxConnectWksocialTwitterProfile in the plugin file.

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close

    Table of Content

    Hide Index