2

Requirements for this task is - I can NOT use any type of components (com_ajax is out of the options list)

GOAL: send post data if checkboxes are 'checked' using ajax within custom module.

  1. Is my AJAX code correct? To be specific, if URL is right? I know that 'defined('_JEXEC') or die;' won't allow to send post data to specific file like this, but even if I comment it out, I don't succeed to get POST data. What could be wrong? I try to access POSTED data in mod_test.php with print_r($_POST);

  2. Is there alternative way to send posted data (not using components) to my module helper.php/mod_test.php file? I'm open to creative brain-storming.

      function function1() {
        var data = $("[name='form1']").serialize();
        $.ajax({
            url: "modules/mod_test/mod_test.php", 
            type: "POST",
            async: true,
            cache: false,
            data: data, 
            success: function(data){ 
                alert(data); 
            }
        });
    }
    

    <form method="post" action="this.form.submit();" name="form1">
        <input type="checkbox" name="checkboxList[]" onclick="function1();" value="1">value1</input>
        <input type="checkbox" name="checkboxList[]" onclick="function1();" value="2">value2</input>
        <input type="checkbox" name="checkboxList[]" onclick="function1();" value="3">value3</input>
    </form>
    

Any real code example would be flawless.

2

You should use Joomla`s com_ajax.

The way to achieve your goal is:

Change url to

index.php?option=com_ajax&ignoreMessages&module=YMODULE&method=YMETHOD&format=json

Also you need to create module helper at:

/modules/mod_YMODULE/helper.php

and place class like ModYMODULEHelper there.

Last step is to place processing method there like: public function YMETHODAjax(){return true;}

That's pretty much all.


to simplify and use default values:

url => index.php?option=com_ajax&module=YMODULE&format=json

processing method to use => public function getAjax(){return true;}

But this without &ignoreMessages you'll not receive system messages from JFactory::getApplication()->enqueueMessage()


Also you can check the plugin strategy I've described in answer to another post: Using AJAX in a custom module - how to do it?

0

What Alexandr wrote is correct. Just 2 things:

1.- No menu id is it necessary

2.- Don't do as me: Please register module YMODULE you created in table extensions (remember to add mod )

  • What do you mean "No menu id is it necessary"? I don't see anyone mentioning a menu id on this page. Please improve that sentence. – mickmackusa Oct 5 '18 at 10:49
  • Comments are not for extended discussion; this conversation has been moved to chat. – Lodder Oct 8 '18 at 11:05
-1
  1. add a slash (presuming yr joomla is at the root of domain) to the start of your ajax url.
  2. make your ajax request to a file all by itself rather than using a joomla file - obviously if anything calls something from the joomla framework in that file it will throw an error because the framework wont be loaded. you can load it yourself manually of course, but then my question would be :
  3. what's wrong with mod_ajax given that it suits this use case?
  4. are you fully confident your js function is running? is it alerting the html of a 404 page?

anyway - here is a working code example. the respond.php is just

<?php print_r($_POST);

and then mod_test.php is:

  <form method="post" action="this.form.submit();" name="form1">
    <input type="checkbox" name="checkboxList[]" onclick="function1();" value="1">value1</input>
    <input type="checkbox" name="checkboxList[]" onclick="function1();" value="2">value2</input>
    <input type="checkbox" name="checkboxList[]" onclick="function1();" value="3">value3</input>
</form>
<script>    
function function1() {
    var data = jQuery("[name='form1']").serialize();
    jQuery.ajax({
        url: "/modules/mod_test/respond.php", 
        type: "POST",
        async: true,
        cache: false,
        data: data, 
        success: function(data){ 
            alert(data); 
        }
    });
}
</script>
  • In Joomla you can't set the URL as a PHP file – Lodder Apr 16 '17 at 10:16
  • My answer involves bypassing Joomla in order to get the ajax call to work as simply as possible. I point out the limitations of this in item 2 of my answer. – jamesgarrett Apr 16 '17 at 23:18

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.