使用ajax保存模块参数的模块开发问题

发布于 2024-11-04 19:58:44 字数 4654 浏览 0 评论 0原文

我对 Joomla 真的很陌生,而且 1.6 似乎也很新,所以我在将数据保存到模块后端的模块参数字段中时遇到了一些麻烦。

让我更好地解释一下。

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

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

基本上我的流程如下。

  1. 我进入 Joomla 的模块管理器 行政。
  2. 我选择我安装的模块并让 它打开了。
  3. 代码运行以填充 带有列表数量的下拉框 联系人可以订阅的名称。
  4. 最重要的 - 我希望能够 选择列表名称并保存 ID 进入模块参数字段 Joomla DB onchange 与 ajax 请求 -

我让 onchange 事件正常工作,甚至使用 post 请求运行脚本,但在我用来处理 post 并保存数据的 PHP 文件中,我无法让数据库实例对其执行任何操作。这就是解释,代码如下:

模块后端的自定义字段

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';

}

Javascript

// 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'));

}

我希望你能给我一些关于如何解决这个问题的建议我被困住了。唯一的限制是它必须是一个模块。老板的命令。

I am really new to Joomla and 1.6 is also very new it seems so I am having a bit of trouble to save data into the module params field in the module back end.

Let me explain a bit better.

I have a small contacts manager that I want people to subscribe to using a joomla module in Joomla 1.6. Now the module is created and everything works fine. I even got it right to create a custom field in the module back end that calls my API and populates the dropdown box with the data needed.

I want to be able to save the selection as a module param to easily access it in my template. This seems to be very hard to do because I cannot find any documentation on it whatsoever.

Basically my process is as follows.

  1. I go to the module manager in Joomla
    admin.
  2. I select my installed module and let
    it open.
  3. The Code runs to populate the
    dropdown box with an amount of list
    names a contact can subscribe to.
  4. The kicker - I want to be able to
    select a list name and save an ID
    into the module params field in the
    Joomla DB onchange with an ajax
    request -

I get the onchange event to work and even run the script with the post request but in the PHP file I use to handle the post and save the data I cannot get the DB instance to do any operations on it. Thats the explanation, here's the code :

custom field in the module backend

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
        );

    }

}

The utility file that saves the param

$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';

}

The Javascript

// 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'));

}

I hope you can give me some advise as how to this I am stuck like crazy. The only constraint is that it HAS to be a module. Boss's orders.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

や三分注定 2024-11-11 19:58:44

似乎没有人涉及这个问题。

我找到了一种方法来修复它,但不使用ajax。

Looks like no one relates to this question.

I have found a way to fix it though by NOT using ajax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文