简体   繁体   中英

How to get data from an input page in Joomla and use it in a controller

I have a Joomla xml, defining these two fields among others:

<field
    name="country_code"
    type="sql"
    default="10"
    label="COM_TEAM_COUNTRY_CODE"
    query="SELECT country_code, country FROM #__team_country"
    key_field="country_code"
    value_field="country"
    />  
<field name="town" 
    type="text" 
    size="120"  
    class="inputbox span6"
    label="COM_TEAM_FIELD_TOWN_LABEL" 
    description="COM_TEAM_FIELD_TOWN_DESC" 
    required="true" />

These two fields are updated using a function, together with the rest:

<?php
defined('_JEXEC') or die;
class TeamControllerTeam extends JControllerForm
{

    function saveAndSetCity()
    {
            parent::save();
            $jinput = JFactory::getApplication()->input;
            // Get a db connection.
            $db = JFactory::getDbo();


            // Create a new query object.
            $query = $db->getQuery(true);

            // Insert columns.
            $columns = array('town', 'country_code');
            $newCCode  = $jinput->get('country_code','','');
            // Insert values.
            $values = array($db->quote('newTown'), $db->quote($newCCode));

            // Prepare the insert query.
            $query
            ->insert($db->quoteName('#__team_city'))
            ->columns($db->quoteName($columns))
            ->values(implode(',', $values));

            // Set the query using our newly populated query object and execute it.
            $db->setQuery($query);
            $db->execute();
    }
}

The Parent::save line stores it in the table, but I would like to store the specific two fields in another table as well, and the code that I have found and adapted works fine, except for getting the actual value that I want to store, as I haven't found a method that can get the values. The code I have made only stores a fixed literal, or blanks no matter how I try. I have read the Joomla documentation page about Jinput, but I did not understood how to use it.

Kind regrads Peter Durup

I found out that I was using JInput in the wrong way, and I have changed the code so it now works.

<?php
defined('_JEXEC') or die;
class TeamControllerTeam extends JControllerForm
{

    function saveAndSetCity()
    {
            parent::save();
            $input = JFactory::getApplication()->input;
            $formData = new JInput($input->get('jform','', 'array'));
            // Get a db connection.
            $db = JFactory::getDbo();


            // Create a new query object.
            $query = $db->getQuery(true);

            // Insert columns.
            $columns = array('town', 'country_code');
            //$newCCode  = $jinput->get('country_code','','STR');
            $newTown = $formData->getWord('town');
            $newCCode = $formData->getWord('country_code');
            // Insert values.
            $values = array($db->quote($newTown), $db->quote($newCCode));

            // Prepare the insert query.
            $query
            ->insert($db->quoteName('#__team_city'))
            ->columns($db->quoteName($columns))
            ->values(implode(',', $values));

            // Set the query using our newly populated query object and execute it.
            $db->setQuery($query);
            $db->execute();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM