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

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

$
0
0
Hi SharkyKZ,

Again Thankyou so much for your help.

I had to make some further adjustments to the code after doing the amendments you suggested above as I started to get "internal server error" returned, It appeared to have fixed one issue but then allowed to code to move on further to another bit I had got wrong.

However after some further tweeks I now have it up and working so to finish off this thread incase this helps anyone trying to do something similar below is the code I ended up with in the files which work in Joomla 5.0.2

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,                //I add this piece of codebeforeSend: function () {$(".search-result").append("Please Wait...<br>");},//I add this piece of code to check errorerror:  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\Factory;class modMyModHelper{    public static function getAjax()    {        $input = Factory::getApplication()->input;        $data  = $input->get('data');            if (!$data)        {            return 'Please enter a valid value...';        }        // Connect to database        $db = Factory::getDbo();        $query = $db->getQuery(true);        // Build the query        $query            ->select($db->quoteName('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();if ($result)        {            return "<a>" . $result . "</a>";        }return 'Sorry! No result for your search.';    }}
/tmpl/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>
my_mod.xml

Code:

<?xml version="1.0" encoding="utf-8"?><extension type="module" version="4.0" client="site" method="upgrade">    <name>Your Module Name</name>    <creationDate>07 February, 2024</creationDate>    <author>Your Name</author>    <authorUrl>https://Your Site</authorUrl>    <copyright>Copyright (C) 2024 Your Cool Copyright. All rights reserved.</copyright>    <license>GNU General Public License version 3, or later.</license>    <version>1.0.0</version>    <description>Your Module Description</description>    <install><sql><file driver="mysql" charset="utf8">sql/install.mysql.sql</file></sql></install><uninstall><sql><file driver="mysql" charset="utf8">sql/uninstall.mysql.sql</file></sql></uninstall><update><schemas><schemapath type="mysql">sql/updates/mysql</schemapath></schemas></update>    <files>        <filename module="mod_my_mod">mod_my_mod.php</filename>        <filename>mod_my_mod.xml</filename>        <filename>helper.php</filename>        <folder>tmpl</folder>        <folder>sql</folder>    </files></extension>

Statistics: Posted by Martyn1 — Thu Feb 08, 2024 10:51 pm



Viewing all articles
Browse latest Browse all 2131

Trending Articles