1

I have a fully working module in Joomla, but I'm struggling with the last piece which is sending a value from Javascript in my default.php file into the helper file, where it will be used in a sql query and return the results. Everything up to that point works perfectly and if I just hardcode the value in SQL it returns what I need to the default.php file.

My issue is that when the JS function getApi is called and I set an input value to 12345, I also want to send that value of 12345 into my helpepr function called getStores

How can I properly send this value and have it returned in real time asynchronously?

helper.php

<?php

class modTestHelper
{

public static function getStores($zip)
{
    $db = JFactory::getDBO();
    $user = JFactory::getUser();
    
    $app = JFactory::getApplication();
    $lang = JFactory::getLanguage();
    $date = JFactory::getDate();
    $now = $date->format('Y-m-d');
    
    
    $query = 'SELECT
            name,
            phone,
            address,
            city,
            state,
            zip,
            country,
            url,
            logo,
            lat,
            lng

            from #__loc_table
            where published = 1
            AND zip ="'.$zip.'"';
    
    
    $db->setQuery($query);
    $item=$db->loadObjectList();
    $stores = [
        'type' => 'FeatureCollection',
        'features' => array_map(function($i) {
            return [
                'type' => 'Feature',
                'geometry' => [
                    'type' => 'Point',
                    'coordinates' => [
                        $i->lat,
                        $i->lng
                    ]
                ],
                'properties' => [
                    'storeImage' => $i->logo,
                    'storeName' => $i->name,
                    'phoneFormatted' => $i->phone,
                    'address' => $i->address,
                    'city' => $i->city,
                    'country' => $i->country,
                    'postalCode' => $i->zip,
                    'state' => $i->state
                ]
            ];
        }, $item)
    ];                      
            
    return json_encode($stores);
}
}

mod_test.php

<?php 
 defined('_JEXEC') or die;
require_once dirname(__FILE__) . '/helper.php';

$test = modTestHelper::getStores();


require JModuleHelper::getLayoutPath('mod_test', $params->get('layout', 'default'));
?>

default.php

    <script type="text/javascript">

          function getApi() {
             var zip = '12345';
             div.value = var;
          }

     </script>
1
  • I see $now declared, but where is it used?
    – mickmackusa
    Sep 23 at 5:03
1

To pass data between the client side in the browser and your server side component you need to use either AJAX or Web sockets base to communicate. Via a JS function on your page you make a connection with your server and execute the helper function and then return the data to the JS function that then changes the values on your page.

AJAX comes in two flavours these days, the older XMLHttpRequest style or the newer Fetch API that you can set up and call in a JS Function, you can can find plenty of examples and reading material on each of these as well as Web sockets

For receiving the communication on the server side, Joomla has Com_ajax which provides a generic receiving point for your site and then you use a Plugin to forward the request across to your module and helper file to process the request and get the data to be returned. For some reading on com_ajax, https://docs.joomla.org/Using_Joomla_Ajax_Interface and a brief sample https://www.itoctopus.com/how-to-use-joomlas-ajax-interface-component-com_ajax and there are plenty more in a Google search for com_ajax

3
  • Thank you. Ok, so I understand how to call the component com_ajax as well as the module and method in question, however, when crafting the URL in the ajax code how would I specify the endpoint? Could I just use a relative URL path?
    – Geoff_S
    Sep 23 at 11:44
  • this post from a few years ago might help you with the format of the URL, joomla.stackexchange.com/questions/20973/…
    – Irata
    Sep 23 at 12:55
  • Thank you. That got me a bit further but see this question I just posted if you happen to have any tips: joomla.stackexchange.com/questions/31560/…
    – Geoff_S
    Sep 23 at 16:51

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.