簡體   English   中英

如何從Joomla中的輸入頁面獲取數據並在控制器中使用它

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

我有一個Joomla xml,其中定義了這兩個字段:

<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" />

這兩個字段使用函數進行更新,其余的則進行更新:

<?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();
    }
}

Parent :: save行將其存儲在表中,但是我也想將特定的兩個字段也存儲在另一個表中,我找到並改編的代碼可以正常工作,除了獲得想要的實際值外存儲,因為我還沒有找到可以獲取值的方法。 無論我如何嘗試,我編寫的代碼僅存儲固定的文字或空白。 我已經閱讀了有關Jinput的Joomla文檔頁面,但是我不知道如何使用它。

善待彼得·杜魯普

我發現我以錯誤的方式使用了JInput,並且我更改了代碼,因此現在可以使用了。

<?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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM