0

I got a view in my component's frontend, which catches data from model using the ID of the element:

protected function populateState($ordering = null, $direction =  null)
{
    $app        =   JFactory::getApplication();
    $id         =   $app->input->get('id', null, 'int');            
    $this->setState('id', $id);

    parent::populateState();        
}   

/**
 * getItem function.
 * 
 * @access public
 * @param mixed $pk (default: null)
 * @return void
 */
public function getItem($pk = null)
{
    $pk = (!empty($pk)) ? $pk : (int) $this->getState('id');
    ...
    return $this->_item[$pk];
}

Okay, this works fine, the model gets the data and the view is showing it.

Now I would like to create a form which saves the input of a user and attach it to the id.

So I created a form like this

<form id="save" class="form-validate" action="<?php echo JRoute::_('index.php?option=com_bestia'); ?>" method="post" >

... 
<button type="submit" class="btn validate"><?php echo JText::_('COM_BESTIA_SUBMIT', true); ?>
<?php echo JHtml::_( 'form.token' ); ?>
<input type="hidden" name="task" value="item.save" />
</form>

I also created a hidden-field to add the id to the post-data

<field  name        =       "id" 
        type        =       "hidden" /> 

But in my controller I can not access the id like this:

public function save($key = NULL, $urlVar = NULL)
{
    JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
        $jinput         =       JFactory::getApplication()->input;
        $data           =       $jinput->get('jform', array(), 'post', 'array');
}

How can I submit the id to my controller?

  • Is the hidden input displayed in the html? – David Hayes May 4 '16 at 20:04
  • Yes, it is displayed – MyFault May 4 '16 at 21:30
  • 1
    i guess your id is not in 'jform' array, why don't you check what's inside jinput or $_POST, just to make sure you are searching for it in the right place? Or wait a moment, can you copy the actual html where is the id field, as seen in browser? – Marko D May 4 '16 at 23:17
  • Create JTable, yourModelName.xml for form in models/forms/ then in your view.html.php get form $this->form = $this->get('Form'), get item $this->item = $this->get('Item') and if form will not be filled just do $this->form->bind($this->item) and all of the form will be in your scope of data jform or other as you wish in model->getForm() method – Lanah May 5 '16 at 5:29
  • That was my next through as well @MarkoD – David Hayes May 5 '16 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.