1

i am new to joomla.i design a form component for understand mvc pattern like contact form.i form show easily and it is viewable in front end now i want to understand how database is used in joomla.how i send form values to database which files we have to use model view controller.

// no direct access
defined('_JEXEC') or die;
JHtml::_('behavior.keepalive');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');

//Load admin language file
$lang = JFactory::getLanguage();
$lang->load('com_new', JPATH_ADMINISTRATOR);
$document = &JFactory::getDocument();
$document->addStyleSheet(JURI::base() . 'C:\wamp\www\j25\components\com_new\assets\demo.css');
?>
<div class="item_fields">
<?php #if( $this->item ) : ?>
 <form id="form-feild" action="#" method="post" class="form-validate" enctype="multipart/form-data">

<table class="body">
        <tr><td><?php echo $this->form->getLabel('firstname'); ?></td><td></td><td><?php echo $this->form->getInput('firstname'); ?></td></tr>
        <tr><td><?php echo $this->form->getLabel('middlename'); ?></td><td></td><td><?php echo $this->form->getInput('middlename'); ?></td></tr>
        <tr><td><?php echo $this->form->getLabel('lastname'); ?></td><td></td><td><?php echo $this->form->getInput('lastname'); ?></td></tr>
        <tr><td><?php echo $this->form->getLabel('gender'); ?></td><td></td><td><?php echo $this->form->getInput('gender'); ?></td></tr>
        <tr><td><?php echo $this->form->getLabel('dateofbirth'); ?></td><td></td><td><?php echo $this->form->getInput('dateofbirth'); ?></td></tr>
        <tr><td><?php echo $this->form->getLabel('address'); ?></td><td></td><td><?php echo $this->form->getInput('address'); ?></td></tr>
        <tr><td><?php echo $this->form->getLabel('state'); ?></td><td></td><td><?php echo $this->form->getInput('state'); ?></td></tr>
        <tr><td><?php echo $this->form->getLabel('postcode'); ?></td><td></td><td><?php echo $this->form->getInput('postcode'); ?></td></tr>
        <tr><td><?php #echo 'index.php?option=com_details&task=feild.save'; ?></td></tr>    
    <table>   
 <div>
    <button type="submit" class="validate"><span><?php echo JText::_('JSUBMIT'); ?></span></button>
    <?php echo JText::_('or'); ?>
<a href="<?php echo JRoute::_('index.php?option=com_new&task=new.cancel'); ?>" title="<?php echo JText::_('JCANCEL'); ?>"><?php echo JText::_('JCANCEL'); ?></a>

    <input type="hidden" name="option" value="com_new" />
    <input type="hidden" name="task" value="new.save" />
    <?php echo JHtml::_('form.token'); ?>
    </div>
    </form>
 note:->how action work on joomla.In few files i find like JRoute::_('index.php?option=com_new&task=new.save')

help me thank in advance

4

1 回答 1

3

您好这里有在 joomla 中使用 mvc 模式构建表单的官方文档。

表单进入您的 tmp/default.php (site/views/updhelloworld/tmpl/default.php),然后设置您的操作,当您提交表单时,您将“重定向”到控制器 (site/controllers/updhelloworld.php),在这里你必须检查你的表格数据

    $data = JRequest::getVar('jform', array(), 'post', 'array');

然后在您的模型中使用该操作

    $upditem        = $model->updItem($data);

在您的模型中,您使用您的数据库。

 public function updItem($data)
    {
    // set the variables from the passed data
    $id = $data['id'];
    $greeting = $data['greeting'];

    // set the data into a query to update the record
            $db             = $this->getDbo();
            $query  = $db->getQuery(true);
    $query->clear();
            $query->update(' #__helloworld ');
            $query->set(' greeting = '.$db->Quote($greeting) );
            $query->where(' id = ' . (int) $id );

            $db->setQuery((string)$query);

    if (!$db->query()) {
        JError::raiseError(500, $db->getErrorMsg());
            return false;
    } else {
            return true;
            }
    }
于 2013-03-26T15:37:29.920 回答