1

I'm trying to create my own ajax module. When I submit the form I'm getting a 404 post error. Any idea what went wrong?

This is what I have done so far.

mod_ajax_test.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module"
           version="3.0"
           method="upgrade">
    <name>Ajax Test</name>
    <creationDate>February 7, 2017</creationDate>
    <author>Striker8472</author>
    <authorUrl>http://doa-x.com</authorUrl>
    <copyright>Copyright (C) 2017 doa-x.com. All rights reserved.</copyright>
    <license>GNU General Public License version 2, or later.</license>
    <version>0.0.1</version>
    <description>Ajax Test</description>
    <files>
        <filename module="mod_ajax_test">mod_ajax_test.php</filename>
        <filename>helper.php</filename>
        <folder>tmpl</folder>
    </files>
</extension>

mod_ajax_text.php

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

/**
 * File       mod_ajax_test.php
 * Created    2/7/17 17:12 PM
 */

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

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

$js = <<<JS
(function ($) {
    $(document).on('click', 'input[type=submit]', function () {
        var value   = $('input[name=data]').val(),
            request = {
                    'option' : 'com_ajax',
                    'module' : 'mod_ajax_test',
                    'data'   : value,
                    'format' : 'raw'
                };
        $.ajax({
            type   : 'POST',
            data   : request,
            success: function (response) {
                $('.status').html(response);
            }
        });
        return false;
    });
})(jQuery)
JS;

$doc->addScriptDeclaration($js);

require JModuleHelper::getLayoutPath('mod_ajax_test');

helper.php

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

/**
 * File       helper.php
 * Created    2/7/17 17:12 PM
 */
class modAjaxTestHelper
{
    public static function getAjax()
    {
        $input = JFactory::getApplication()->input;
        $data  = $input->get('data');

        return 'Your text here: ' . $data . '!';
    }
}

tmpl/default.php

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

/**
 * File       default.php
 * Created    2/7/17 17:12 PM
 */

?>
<form>
    <input type="text" name="data">
    <input type="submit" />
</form>
<div class="status"></div>
2

Found the problem by myself.

'module' : 'mod_ajax_test',

must be

'module' : 'ajax_test',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.