douchang8758 2016-12-31 09:00
浏览 19

如何从Joomla中的输入页面获取数据并在控制器中使用它

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

  • 写回答

1条回答 默认 最新

  • dongtan4046 2016-12-31 14:22
    关注

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

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口