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