Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 2350

Joomla! 5.x Coding • Re: Custom Module not working unless logged in

$
0
0
Ok, so taking sharkyKZ's advice I have had a go at recreating my work but using Ajax. It has been difficult for my limited knowledge as it appears most examples are for older versions of joomla and this is my first time dabbling with ajax.

I settled on starting with this https://github.com/josedaboinv/Ajax-Sea ... mla-Module and after some editing have managed to get it to install on Joomla 5 and appear to work to a point.

The issue I am now having is that no matter what I put in the search I get the 'Sorry! No result for your search.' message. I get this even if I enter a number I know should return a result from the database. Can anyone advise where my limited knowledge has caused errors in the code or if I am way off.

mod_my_mod.php

Code:

<?phpdefined('_JEXEC') or die;use Joomla\CMS\Factory;use Joomla\CMS\Helper\ModuleHelper;include_once __DIR__ . '/helper.php';// Instantiate global document object$doc = Factory::getDocument();$js = <<<JS(function ($) {    $(document).on('click', 'input[type=submit]', function () {    var value   = $('input[name="data"]').val(),            request = {                    'option' : 'com_ajax',                    'module' : 'my_mod',                    'data'   : value,                    'format' : 'raw'                };        $.ajax({            type   : 'POST',            data   : request,            beforeSend: function () {$(".search-result").append("Please Wait...<br>");},error:  function (response) {                 $(".search-result").append('Error ajax: '+response.statusText );},            success: function (response) {                $('.search-result').html(response);            }        });        return false;    });})(jQuery)JS;$doc->addScriptDeclaration($js);require ModuleHelper::getLayoutPath('mod_my_mod');
helper.php

Code:

<?phpdefined('_JEXEC') or die;use Joomla\CMS\Router\Route;use Joomla\CMS\Factory;use joomla\CMS\ContentHelperRoute;class modMymodHelper{    public static function getAjax()    {        $input = Factory::getApplication()->input;        $data  = $input->get('data');        // Connect to database        $db = Factory::getDbo();        $query = $db->getQuery(true);        // Build the query        $query            ->select('period')            ->from($db->quoteName('#__gardnerperiod'))            ->where($db->quoteName('first') . "<=" . $db->quote($data))            ->andwhere($db->quoteName('last') . ">=" . $db->quote($data));        $db->setQuery($query);        $result = $db->loadResult();        // Get output        $output = null;        if($output != null or empty($data))        {            $output .= '<h4><a> href="' . ContentHelperRoute::getArticleRoute($result) . '"></a></h4>';        }        else                {            $output = 'Sorry! No result for your search.';         }        return $output;    }}
default.php

Code:

<?php defined('_JEXEC') or die;use Joomla\CMS\Factory;$document = Factory::getDocument(); //I add this piece of code$document->addScript('//code.jquery.com/jquery-latest.min.js');  //I add this piece of code?><form>    <input type="text" name="data" />    <input type="submit" value="Search" /></form><div class="search-result"></div>

Statistics: Posted by Martyn1 — Wed Feb 07, 2024 3:21 pm



Viewing all articles
Browse latest Browse all 2350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>