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>