简体   繁体   English

如何在joomla 2.5模块中使用AJAX?

[英]How to use AJAX in joomla 2.5 module?

I have three drop down in my module.I want to call ajax on drop down onChange() event in my joomla 2.5 module. 我的模块中有三个下拉菜单,我想在我的joomla 2.5模块中的下拉onChange()事件中调用ajax。 Country,State,City are drop down. 国家,州,城市正在下降。 user select country from first drop down when all state result display in second drop down using ajax. 当所有状态结果都使用ajax在第二个下拉列表中显示时,用户从第一个下拉列表中选择国家。

How to do above functionality using AJAX in joomla 2.5 module. 如何在joomla 2.5模块中使用AJAX实现上述功能。

Please help me. 请帮我。

There is no official way to do ajax from a module. 没有正式的方法可以从模块执行ajax。
If you where to develop a component you could call your component with index.php?option=com_yourcomponent&task=getjsondata&view=yourview&format=json 如果您在哪里开发组件,则可以使用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() 这样,您的文件views/yourview/view.json.php将被调用,在那里您可以将数据发送出去echo json_encode( array("success"=>true) ); jexit() echo json_encode( array("success"=>true) ); jexit() please note the use of jexit() insted o exit() echo json_encode( array("success"=>true) ); jexit()请注意,使用jexit() exit()

If you really need to do ajax from a module here's a hack to import joomla in your files 如果您真的需要从模块执行Ajax,可以在文件中导入joomla

<?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. 在您的模块文件夹中创建一个与上述文件类似的文件(对我来说save_email.php ),然后直接调用它。 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM