繁体   English   中英

从joomla前端保存数据

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM