[英]saving data from joomla frontend
我一直在尋找如何從joomla前端保存數據的解決方案。 我遇到了以下控制器和模型的代碼,它完美地運行。 但我正在尋找一種標准的做法,比如它在后端使用jform,jtable等完成......在下面的代碼(內部模型)中,保存技術看起來並不那么吸引人。 我完全不知道服務器端驗證是如何實現的。
這可能會令人困惑,所以我想重申一下,在后端我們甚至不必編寫添加或保存或更新功能,它由核心類自動處理,同時進行客戶端和服務器端驗證。 所以我一直在尋找類似的東西。
調節器
<?php
// No direct access.
defined('_JEXEC') or die;
// Include dependancy of the main controllerform class
jimport('joomla.application.component.controllerform');
class JobsControllerRegistration extends JControllerForm
{
public function getModel($name = 'Registration', $prefix = 'JobsModel', $config = array('ignore_request' => true))
{
return parent::getModel($name, $prefix, array('ignore_request' => false));
}
public function submit()
{
// Check for request forgeries.
JRequest::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
// Initialise variables.
$app = JFactory::getApplication();
$model = $this->getModel('Registration');
// Get the data from the form POST
$data = JRequest::getVar('jform', array(), 'post', 'array');
$form = $model->getForm();
if (!$form) {
JError::raiseError(500, $model->getError());
return false;
}
// Now update the loaded data to the database via a function in the model
$upditem = $model->updItem($data);
// check if ok and display appropriate message. This can also have a redirect if desired.
if ($upditem) {
echo "<h2>Joining with us is successfully saved.</h2>";
} else {
echo "<h2>Joining with us faild.</h2>";
}
return true;
}
}
模型
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// Include dependancy of the main model form
jimport('joomla.application.component.modelform');
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
// Include dependancy of the dispatcher
jimport('joomla.event.dispatcher');
/**
* HelloWorld Model
*/
class JobsModelRegistration extends JModelForm
{
/**
* @var object item
*/
protected $item;
/**
* Get the data for a new qualification
*/
public function getForm($data = array(), $loadData = true)
{
$app = JFactory::getApplication('site');
// Get the form.
$form = $this->loadForm('com_jobs.registration', 'registration', array('control' => 'jform', 'load_data' => true),true);
if (empty($form)) {
return false;
}
return $form;
}
//Nwely added method for saving data
public function updItem($data)
{
// set the variables from the passed data
$fname = $data['fname'];
$lname = $data['lname'];
$age = $data['age'];
$city = $data['city'];
$telephone = $data['telephone'];
$email = $data['email'];
$comments = $data['comments'];
// set the data into a query to update the record
$db = $this->getDbo();
$query = $db->getQuery(true);
$query->clear();
$db =& JFactory::getDBO();
$query = "INSERT INTO #__joinwithus ( `id`, `firstname`, `lastname`, `age`, `city`, `telephone`, `email`, `comment`)
VALUES (NULL,'" . $fname . "','" . $lname . "','" . $age . "','" . $city . "','" . $email . "','" . $telephone . "','" . $comments . "')";
$db->setQuery((string)$query);
if (!$db->query()) {
JError::raiseError(500, $db->getErrorMsg());
return false;
} else {
return true;
}
}
}
有人可以指點我一個很好的教程或分享一個組件,與joomla 2.5在前端處理表單。
在模型中使用以下代碼
$data = $app->input->getArray($_POST);
$query = $db->getQuery(true);
您應該能夠直接使用jcontrollerform的方法,而不是像您一樣編寫自己的submit()方法(和updItem())。 我在這里描述類似的東西 。 這意味着您使用jform以常規方式顯示表單,並使用action =“index.php?option = com_jobs&task = save&view = registration&id = whateverid”
這樣使用jcontrollerform-> save(),它又調用模型的save()。 (嗯,這可能意味着你的模型應該擴展JModelAdmin而不是JModelForm,以包含相關的方法。)這將運行所有必要的驗證檢查等。
您可能需要為要使用的模型,表格和表單注冊路徑,就像我在鏈接中一樣。
如果編輯現有數據,則需要在url參數中包含id,因為jform [id] - 參數將被忽略。
對不起,我沒有任何好的教程或其他適合你,希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.