1
votes

I have three drop down in my module.I want to call ajax on drop down onChange() event in my joomla 2.5 module. Country,State,City are drop down. user select country from first drop down when all state result display in second drop down using ajax.

How to do above functionality using AJAX in joomla 2.5 module.

Please help me.

1

1 Answers

0
votes

There is no official way to do ajax from a module.
If you where to develop a component you could call your component with index.php?option=com_yourcomponent&task=getjsondata&view=yourview&format=json

This way your the file views/yourview/view.json.php would be called and there you can get your data send it out echo json_encode( array("success"=>true) ); jexit() please note the use of jexit() insted o exit()

If you really need to do ajax from a module here's a hack to import joomla in your files

<?php
if (! class_exists('JFactory') ) {
    define( 'DS', DIRECTORY_SEPARATOR );
    $rootFolder = explode(DS,dirname(__FILE__));
    $currentfolderlevel = 2;

    array_splice($rootFolder,-$currentfolderlevel);

    $base_folder = implode(DS,$rootFolder);


    if(is_dir($base_folder.DS.'libraries'.DS.'joomla'))
    {
        define( '_JEXEC', 1 );
        define('JPATH_BASE',implode(DS,$rootFolder));

        require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
        require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

        // Joomla is loaded! horray!
        $email = JRequest::getVar('email');
        $tags = implode(",",JRequest::getVar('tags',array()));

        $db = JFactory::getDbo();
        $db->setQuery("INSERT INTO `#__newsletter_users` (email,tags) VALUES('$email','$tags')");
        $db->query();

    }
}

?>

create a file like the one above (save_email.php for me) in your module folder and call it directly. Here is a piece of my modules template:

$.get('<?php echo JUri::base()."modules/mod_newsletter/save_email.php?email=" ?>'+email, function(data) {
    console.log(data);
    modal.hide();
});