duanchai0028 2013-04-15 13:20
浏览 67

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

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条回答 默认 最新

  • dongliang1941 2013-12-05 10:57
    关注

    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();
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?