Caynadian
Caynadian

Reputation: 757

Joomla 3.x Get Module Parameters Within Ajax Call

I am playing around with a custom build module in Joomla 3.x that uses Ajax to display some real-time data. Within the function executed by the Ajax call I need to access the module parameters and I'm not sure how to do this. Normally, I could just put $var = $params('paramName', 'default') in the module PHP file to get the param but this is not available when the call is made by Ajax. This is my template code that executes the Ajax call:

<script type="text/javascript">
  jQuery(document).ready(function() {
    jQuery.get('index.php?option=com_ajax&module=whatsinport&method=getWhatsInPort&format=json', function(data) {
      console.log(data);
      var response = jQuery.parseJSON(data);

This is the code in my helper.php class:

class modWhatsInPortHelper {

  public static function getWhatsInPortAjax() 
  {
    $results = array();
    $results['status'] = 'ok';

    $app = JFactory::getApplication();
    $serverName = $app->getCfg('mod_whatsinport_serverName');
    $dbName = $app->getCfg('mod_whatsinport_dbName');
    $dbUser = $app->getCfg('mod_whatsinport_dbUser');
    $dbPwd = $app->getCfg('mod_whatsinport_dbPwd');

    $connectionInfo = array( "Database"=>$dbName, "UID"=>$dbUser, "PWD"=>$dbPwd, "ReturnDatesAsStrings"=>true);
    $conn = sqlsrv_connect( $serverName, $connectionInfo);

The $app->getCfg does not appear to do what I want - I am guessing it is only used for Joomla config settings. I also tried:

  $app = JFactory::getApplication();
  $params = $app->getParams();
  $serverName = $params->get('mod_whatsinport_serverName');
  $dbName = $params->get('mod_whatsinport_dbName');
  $dbUser = $params->get('mod_whatsinport_dbUser');
  $dbPwd = $params->get('mod_whatsinport_dbPwd');

But this didn't work either. I forgot to include my module config file:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>WhatsInPort</name>
    <author>Chris Krohn</author>
    <version>1.0.0</version>
    <description>Displays a list of current vessels in port.</description>
    <files>
        <filename>mod_whatsinport.xml</filename>
        <filename module="mod_whatsinport">mod_whatsinport.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <config>
      <fields name="params">
        <fieldset name="basic">
          <field name="mod_whatsinport_serverName" type="text" default="" label="Server Name" description="NETBIOS name of the database server to connect to." />
          <field name="mod_whatsinport_dbName" type="text" default="" label="Database Name" description="Name of the database with the HMLOG table." />
          <field name="mod_whatsinport_dbUser" type="text" default="" label="Database User" description="User name to login to the server with." />
          <field name="mod_whatsinport_dbPwd" type="password" default="" label="Database Password" description="Password to login to the server with." />
        </fieldset>
      </fields>
    </config>
</extension>

Upvotes: 1

Views: 1949

Answers (2)

Rodrigo Chambi
Rodrigo Chambi

Reputation: 11

Retrieve data of a parameter within the getjax() method.

public static  function getAjax(){
    $app = JFactory::getApplication();
    $module = JModuleHelper::getModule('my_module');
    $params = new JRegistry($module->params);
    return $params ->get('myparams');
}

Upvotes: 1

Caynadian
Caynadian

Reputation: 757

Figured it out. Should be:

  $app = JFactory::getApplication();
  $module = JModuleHelper::getModule('mod_whatsinport','WhatsInPort');
  $params = new JRegistry($module->params);       
  $serverName = $params->get('mod_whatsinport_serverName');
  $dbName = $params->get('mod_whatsinport_dbName');
  $dbUser = $params->get('mod_whatsinport_dbUser');
  $dbPwd = $params->get('mod_whatsinport_dbPwd');

Upvotes: 3

Related Questions