Php 使用ajax保存模块参数时出现模块开发问题

Php 使用ajax保存模块参数时出现模块开发问题,php,ajax,joomla-extensions,joomla1.6,Php,Ajax,Joomla Extensions,Joomla1.6,我是Joomla的新手,1.6也是非常新的,所以我在将数据保存到模块后端的模块参数字段时遇到了一些麻烦 让我解释清楚一点 我有一个小型联系人管理器,我希望人们使用joomla 1.6中的joomla模块订阅它。现在模块已经创建,一切正常。我甚至正确地在模块后端创建了一个自定义字段,用于调用我的API并用所需的数据填充下拉框 我希望能够将选择保存为模块参数,以便在模板中轻松访问它。这似乎很难做到,因为我找不到任何关于它的文档 基本上我的过程如下 我去找Joomla的模块经理 管理 我选择已安装的模

我是Joomla的新手,1.6也是非常新的,所以我在将数据保存到模块后端的模块参数字段时遇到了一些麻烦

让我解释清楚一点

我有一个小型联系人管理器,我希望人们使用joomla 1.6中的joomla模块订阅它。现在模块已经创建,一切正常。我甚至正确地在模块后端创建了一个自定义字段,用于调用我的API并用所需的数据填充下拉框

我希望能够将选择保存为模块参数,以便在模板中轻松访问它。这似乎很难做到,因为我找不到任何关于它的文档

基本上我的过程如下

我去找Joomla的模块经理 管理 我选择已安装的模块并让 它打开了。 代码运行以填充 带有列表数量的下拉框 联系人可以订阅的姓名。 踢球者-我希望能够 选择列表名称并保存ID 输入到中的模块参数字段中 Joomla DB onchange与ajax 请求- 我让onchange事件工作,甚至用post请求运行脚本,但在我用来处理post和保存数据的PHP文件中,我无法让DB实例对其执行任何操作。这就是解释,下面是代码:

模块后端中的自定义字段

保存参数的实用程序文件

Javascript


我希望你能给我一些建议,作为如何做到这一点,我陷入疯狂。唯一的限制是它必须是一个模块。老板的命令。

看来没有人和这个问题有关


我已经找到了一种方法来解决这个问题,不过不使用ajax。

lol我可以说,再也没有人喜欢Joomla了;
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
jimport('joomla.html.html');
//import the necessary class definition for formfield
jimport('joomla.form.formfield');

// Include API utility file
require_once(dirname(__FILE__) . '/../lib/pmailer_subscription_api.php');

/**
 * Defines the JFormFieldLists class for the pMailer subscription module for
 * Joomla CMS version 1.6 to get the lists provided the API key.
 *
 * @category  Joomla
 * @package   Modules
 * @copyright 2011 Prefix Technologies Pty (Ltd) (http://www.prefix.co.za/)
 * @link      http://www.pmailer.co.za/
 */

class JFormFieldLists extends JFormField
{
    /**
     * The form field type.
     *
     * @var  string
     * @since 1.6
     */
    protected $type = 'lists'; //the form field type

    /**
     * Method to retrieve the lists that resides in your pMailer account using
     * the API.
     *
     * @return array The field option objects.
     * @since 1.6
     */
    protected function getInput()
    {
        $document = JFactory::getDocument();
        $document->addScript(
            JURI::base() . '../modules/mod_pmailer_subscription/lib/'
            . 'mod_pmailer_subscription.js'
        );

        $options = array();
        $attr = '';

        /*
         * Initialize JavaScript field attributes. This is the path to the
         * ajax handler that will save your list selection.
         */
        $attr .= $this->element['onchange'] = (string)
            'onchange="javascript: saveListSelection(\''
                . JURI::base()
                . '../modules/mod_pmailer_subscription/lib/utils.php'
            . '\')"';

        // Get the database instance
        $db = JFactory::getDbo();
        // Build the select query
        $query = 'SELECT params FROM jos_modules'
            . ' WHERE module="mod_pmailer_subscription"';
        $db->setQuery($query);
        $params = $db->loadObjectList();

        // Decode the options to get thje api key and url
        $options = json_decode($params[0]->params, true);

        // Create a new API utility class
        $api = new PMailerSubscriptionApiV1_0(
            $options['enterprise_url'],
            $options['pmailer_api_key']
        );

        // Get the lists needed for subscription
        $response = $api->getLists();

        // Make a default entry for the dropdown
        $lists = array('0' => 'Please select a list');

        // Builds the options for the dropdown
        foreach ( $response['data'] as $list )
        {
            $lists[$list['list_id']] = $list['list_name'];

        }

        // The dropdown output
        return JHTML::_(
            'select.genericlist',
            $lists,
            'mod_pmailer_lists_box',
            trim($attr),
            'id',
            'title',
            $this->value
        );

    }

}
$db = JFactory::getDbo();

if ( (isset($_POST['op']) === true) && ($_POST['op'] === 'saveList') )
{
    $query = 'SELECT params FROM jos_modules'
        . ' WHERE module="mod_pmailer_subscription"';
    $db->setQuery($query);
    $params = $db->loadObjectList();

    // Decode the options to get thje api key and url
    $options = json_decode($params[0]->params, true);

    $options['list_selection'] = (int)$_POST['id'];

    $new_params = json_encode($options);

    $query = 'UPDATE jos_modules SET params = "' . $new_params
        . ' WHERE module="mod_pmailer_subscription"';

    $db->query($query);

    echo 'success';

}
// Gets called on dropdown change event
function saveListSelection(url)
{
    // Ajax code here to save list selection
    var x = new Request({
        url: url,
        method: 'post'
    }).send('op=saveList&id=' + $('mod_pmailer_lists_box').get('value'));

}