0

With help of this document I am developing a component.I have below code in my edit layout:

<form action="<?php echo JRoute::_('index.php?option=com_resdesk&layout=edit&id=' . (int) $this->item->id); ?>"
    method="post" name="resdesk" id="resdesk" class="form-validate">
</form>

In this form I have 2 buttons as below:

<input type="hidden" name="option" value="com_mycomponent" />
<input type="hidden" name="task" value="mycomponent.submit" />
<button type="submit" class="button" name="showresaults" id="seeavail">
<?php echo JText::_('COM_RESDESK_CHECK_AVAILABLEDESKS'); ?></button>
<button type="submit" class="button" name="save_1" id="savevalue">
<?php echo JText::_('COM_RESDESK_SAVE_VALUES'); ?></button>

One button is used to reload the form fields and another for submit data in database. The problem is that after click event (on any of the buttons), the wrong page loads that is a white page with no controller. According to form action the url should be index.php?option=com_mycomponent&layout=edit&id=xx but the URL is ?layout=edit&id=0.If I remove action tag, again the main page loads but it's empty and white with no controller. Any idea? my controller file is as below:

public function getModel($name = '', $prefix = '', $config = array('ignore_request' => true))
    {
        return parent::getModel($name, $prefix, array('ignore_request' => false));
    }
public function submit()
    {

        If (Isset($_POST['showresaults']))
    {
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app    = JFactory::getApplication();
        $model  = $this->getModel('resdesk');

        // Get the data from the form POST
        $data = JRequest::getVar('jform', array(), 'post', 'array');
        $data   = $app->getUserStateFromRequest('com_resdesk.edit.form.data','data');
         if (!$this->data)
        {
      //Set default values
      $this->data['customername'] = '';
        }   
    }
    else
    {       
        // Check for request forgeries.
        JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

        // Initialise variables.
        $app    = JFactory::getApplication();
        $model  = $this->getModel('resdesk');

        // Get the data from the form POST
        $data = JRequest::getVar('jform', array(), 'post', 'array');

        // Now update the loaded data to the database via a function in the model
       $upditem = $model->updItem($data);
        //sendemail();
    }
    }

The model is as below:

public function getTable($type = 'ResDesk', $prefix = 'resdeskTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }
    public function getForm($data = array(), $loadData = true)
    {
        // Get the form.
        $form = $this->loadForm(
            'com_resdesk.resdesk',
            'resdesk',
            array(
                'control' => 'jform',
                'load_data' => $loadData
            )
        );

        if (empty($form))
        {
            return false;
        }

        return $form;
    }
    public function getScript() 
    {
        return 'site/components/com_resdesk/models/forms/resdesk.js';
    }
    protected function loadFormData()
    {
        // Check the session for previously entered form data.
        $data = JFactory::getApplication()->getUserState(
            'com_resdesk.edit.resdesk.data',
            array()
        );

        if (empty($data))
        {
            $data = $this->getItem();
        }

        return $data;
    }
    public function updItem($data)
    {

        //save in database done correctly

        if (!$db->query()) {
            JError::raiseError(500, $db->getErrorMsg());
            return false;
        } else {
            return true;
        }
    }

the view.html.php is as below:

protected $form = null;
    protected $item;
    protected $script;
    protected $canDo;
    public function display($tpl = null)
    {
        // include settings from the admin backend
        $this->includeAdminEnv();
        // Get the Data
        $this->form = $this->get('Form');
        $this->item = $this->get('Item');
        $this->script = $this->get('Script');
         $app   = JFactory::getApplication();    
        $this->data = $app->getUserStateFromRequest('com_resdesk.edit.form.data','data');
        // Check for errors.
        if (count($errors = $this->get('Errors')))
        {
            JError::raiseError(500, implode('<br />', $errors));

            return false;
        }
        parent::display($tpl);
    }
1

The problem is you are not returning to the edit page in your controller. The way Controllers work is you send them data from your view, they manipulate the date, then after manipulation you are redirected back to where you were (usually with a message of some kind).

In the submit method of your controller add this statement in your IF/ELSE clause.

If (Isset($_POST['showresaults']))
    //ALL THE STUFF YOU HAVE IN YOUR IF CLAUSE
    JFactory::getApplication()->enqueueMessage('This is a Success Message.','success');
}else{
    //ALL THE STUFF IN YOUR ELSE STATEMENT
    JFactory::getApplication()->enqueueMessage('This is the error message.','error');
}

//ADD THIS AFTER YOUR ELSE STATEMENT TO REDIRECT YOU BACK TO THE EDIT FORM
$this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=YOURVIEWNAME&layout=edit&id='.$this->data->id, true));

//MODIFY THE JROUTE LINK ABOVE TO SUIT YOUR NEEDS

You should also look into using newer standards link the JInput method for getting your data instead of the older JRequest::getVar methods.

  • Glad you got it handled. – Terry Carter Jan 1 '17 at 16:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.