3

I think this might be useful in future for many others. First of all, I know that there's com_ajax for this kind of a job, but I do not want to use it. Is there an alternative?

What I got:

default.php

<form method="post" name="filterForm">
    <input type="checkbox" name="checkboxList[]" value="1">value1</input>
    <input type="checkbox" name="checkboxList[]" value="2">value2</input>
    <input type="checkbox" name="checkboxList[]" value="3">value3</input>
</form>


jQuery(function($) { // DOM is now ready

    $("input[type=checkbox]").on("change", function() {
        var data = $("filterForm").serialize();
        $.ajax({
            url: "/modules/mod_mymodule/mymodule.php",
            type: "POST",
            async: true,
            cache: false,
            data: ({data: data}),
            dataType: "text",
        });
    });

});

mymodule.php

//defined('_JEXEC') or die; <-- So it's possible to access this file
print_r($_POST);

I get an empty array. What is wrong?

7
  • Have you defined a route (URL) for your ajax callback ? The URL for the callback will not be /modules/mod_mymodule/mymodule.php Commented Apr 13, 2017 at 8:39
  • Have a look at this: github.com/Joomla-Ajax-Interface/Ajax-Session-Module
    – Lodder
    Commented Apr 13, 2017 at 8:39
  • @jowansebastian Can you give me a quick example? My main goal is to collect checkbox post data and do db data manipulation, so I thought I've to define url as helper.php file or something (where you usually do data manipulations)
    – Poo123
    Commented Apr 13, 2017 at 8:48
  • Sorry to have to ask, but in your code isn't obvious nor in your question, so let's make it clear first. Where and how you get the empty array for $_POST?
    – FFrewin
    Commented Apr 13, 2017 at 10:49
  • @FFrewin Probably because no post data is sent => probably, because url is wrong in ajax function. I'm trying to understand how to fix that so I can fill the post data when checkbox is checked.
    – Poo123
    Commented Apr 13, 2017 at 11:01

3 Answers 3

2

I don't understand why you are trying to bypass the framework. I know it is resources wise but it opens you up to security risks. The other thing is that you can't use framework classes/functions as easily. What I would do is:

  • Instead of using a direct URL to the module file, use current URI (JURI::current() as far as I remember). This will direct a POST data to a view where your module was executed so you have the access to it from your module.
  • Then use JFactory::getApplication()->input in your module to access the post data sent by jQuery request. That way you're in the framework, all the security/filtering options are available and working.
  • Depending on what you want to do with the data you can store it in the database, file or send it by email.
  • If you need to let jQuery know about the result of your actions with the data you can exit the application JFactory::getApplication()->close("OK") then just check the response in jQuery.
1

Relates to Sending POST data within module - AJAX

And I've posted answer there already.

In a two words: use com_ajax.

How to? Check answer in post mentioned above. Or look into the code of com_ajax It's pretty simple: you'll send requests to index.php?option=com_ajax&ignoreMessages&module=YMODULE&method=YMETHOD&format=json Description of module logic is in the post above.

And I'll add call plugin description here:

index.php?option=com_ajax&ignoreMessages&plugin=EVENT&group=PLUGINGROUP&format=json

it will load plugins of group PUGINGROUP (ajax group is default) and trigger the event onAjaxEVENT()

4
  • Please have in mind that user DOES NOT want to use com_ajax. Please read question before you post an answer. Commented Apr 22, 2017 at 12:18
  • Yes, but the only wise decision is to do so. And MAYBE if you'll carefully explain how to - he wouldn't mind to. Because for Joomla main problem is lack of documentation, problems finding it, and issues with those explanations that exists. Also if you'll read comments to question you'll see that user is probably will use any solution that would be understandable. And I believe solution with calling page itself is anyone semi-expirienced could produce, so I personally doubt that this is answer user looking for if he even ask the question. IMHO.
    – Alexandr
    Commented Apr 22, 2017 at 18:10
  • Don't assume. Ask Commented Apr 22, 2017 at 22:14
  • @Alexandr I am pinging to ask for your specific attention to my new question: joomla.stackexchange.com/q/22574/12352 Thanks. Commented Mar 22, 2018 at 6:14
1

if you do not want to use com_ajax you can create your own component to handle the ajax calls, another solution is to use a system plugin which would catch your URL and process it.

if you decide to use your own php file instead of processing it via joomla's main index.php, this could lead to lot's of security issues especially if you distribute your module to various customers/users.

the ideal php firewall would detect php executables, whereas in the ideal site there would be just the index.php. thus disallowing executables outside the 2 index.php in joomla is one of the best security measures in joomla world.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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