Ajax在joomla插件3.0中调用依赖组合框

时间:2015-10-14 08:14:37

标签: ajax joomla

我正在尝试使用joomla插件进行ajax调用。在插件中,有两个组合框:项目级别1和项目级别2.项目级别2取决于项目级别1,并根据项目级别1(表bda_level_2)的选择从MySQL表加载。表bda_level_2包含字段level1_id,它是表bda_level_1(项目级别1)的外键。项目级别1也从MySQL表(表bda_level_1)填充。

ajax调用是一个POST请求,它包含数据字段中的项目级别1 id:名称为l1_id的参数。不知何故,填充第二个组合框的ajax调用似乎失败了。我不知道它是否与请求的格式或从请求中检索参数有关,因为无法记录问题。 这是templ / default.php文件的代码:

<?php defined('_JEXEC') or die;

?>
<form>

        Project level 1:
        <select name="level1" class="level1">
        <option selected="selected">--Select Level 1--</option>
        <?php

        // Get a db connection.
        $db = JFactory::getDbo();

        // Create a new query object.
        $query = $db->getQuery(true);

        // Select records
        $query->select($db->quoteName(array('id', 'level1')));
        $query->from($db->quoteName('bda_level_1'));
        $query->order('level1 ASC');


        // Reset the query using our newly populated query object. 


        $db->setQuery($query);

        if($rows = $db->loadAssocList()){

          foreach($rows as $row){

            $level1_id=$row['id'];
            $data=$row['level1'];

            echo '<option value="'.$level1_id.'">'.$data.'</option>';
          }
        }

        ?>
        </select> 


        <br/><br/>

        Level 2:

        <select name="level2" class="level2">

        <option selected="selected">--Select Level 2--</option>

        </select>


</form>

文件helper.php

<?php defined('_JEXEC') or die;

class modProjectLevelAjaxHelper
{
    public static function getAjax()
    {
        $result = ""; 

        $input = JFactory::getApplication()->input;
        $id  = $input->post->get('data');

        $db = JFactory::getDbo();
        // Create a new query object.

        $query = $db->getQuery(true);
        $conditions = array(
            $db->quoteName('level1_id') . ' = ' . $db->quote($id));

        // Select records
        $query->select($db->quoteName(array('id', 'level2')));
        $query->from($db->quoteName('bda_level_2'));
        $query->where($conditions);
        $query->order('level2 ASC');

        // Reset the query using our newly populated query object. 
        $db->setQuery($query);

        if($rows = $db->loadAssocList()){

        foreach($rows as $row){

          $level2_id=$row['id'];
          $data=$row['level2'];

          $result += '<option value="'.$level2_id.'">'.$data.'</option>';

          }
        }
        return $result;
    }
}

文件mod_project_level_ajax.php

<?php defined('_JEXEC') or die;

// Include the helper.
require_once __DIR__ . '/helper.php';

// Instantiate global document object
$doc = JFactory::getDocument();

$js = <<<JS
(function ($) {
    $('.level1').change(function () {
                var value   = $('.level1').val();
                var dataString = 'l1_id='+value;
                var request = {
                    'option' : 'com_ajax',
                    'module' : 'project_level_ajax',
                    'data'   : dataString,
                   'format' : 'raw'
                };                                
        $.ajax({
            type   : 'POST',
            data   : request,
            success: function (response) {
                $('.level2').html(response);
            }
        });
        return false;
     });
})(jQuery)
JS;

$doc->addScriptDeclaration($js);

require JModuleHelper::getLayoutPath('mod_project_level_ajax');

文件mod_project_level_ajax.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module"
           version="3.0"
           method="upgrade">
    <name>Project Level Ajax</name>
    <creationDate>January 17, 2014</creationDate>
    <author>Matt Thomas</author>
    <authorUrl>http://betweenbrain.com</authorUrl>
    <copyright>Copyright (C) 2013 betweenbrain llc. All rights reserved.      </copyright>
    <license>GNU General Public License version 2, or later.</license>
    <version>3.0.1</version>
    <description>Project Level Ajax!</description>

    <files>
        <filename module="mod_project_level_ajax">mod_project_level_ajax.php</filename>
        <filename>helper.php</filename>
        <folder>tmpl</folder>
    </files>

</extension>

创建表格的脚本:

DROP TABLE IF EXISTS `bda_level_1`;
CREATE TABLE `bda_level_1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,  
  `level1` varchar(200) COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `bda_level_2`;
CREATE TABLE `bda_level_2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `level1_id` int(11),
  `level2` varchar(200) COLLATE utf8_unicode_ci,
  PRIMARY KEY (`id`),
  CONSTRAINT FOREIGN KEY (`level1_id`) REFERENCES `bda_level1`(`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO bda_level_1 (`id`,`level1`) VALUES
(1,'Security'),
(2,'Cyber Security');

INSERT INTO bda_level_2 (`id`,`level2`,`level1_id`) VALUES
(1, 'Access Control Systems',1),
(2, 'Biometrics',1),
(3, 'CCTV & Associated Systems',1),
(4, 'Tracking Systems',1),
(5, 'Sensing Systems and Heads',1),
(6, 'Fire',1),
(7, 'Intruder Alarm Systems',1),
(8, 'Information & Data Security',1),
(9, 'Manned Security Services',1),
(10, 'Security Hardware',1),
(11, 'Weapons & Ammunition',1),
(12, 'Communications',1),
(13, 'Personnel Protection & Control',1),
(14, 'Vehicles',1),
(15, 'Audio Equipment & Systems',1),
(16, 'Ancillary Security Equipment & Services nec',1),
(17, 'Bomb Disposal',1),
(18, 'Forensics & Evidence',1),
(19, 'CBRN & Anti Terrorism',1),
(20, 'Cyber Threat Intelligence',2),
(21, 'System Recovery & Data Cleansing',2),
(22, 'Situational Awareness Products',2);

非常感谢任何帮助。

0 个答案:

没有答案