User Rating: 1 / 5

Star ActiveStar InactiveStar InactiveStar InactiveStar Inactive
 

After building a successful form  And then later we have Save button to save the form data into the database.

- Start editing files in the folder hello.php controllers add method save. 

 

com_helloworld/controllers/hello.php

function save()
{
        
        JRequest::checkToken() or die('Invaild Token');

       
        $data = $this->input->post->get('jform', array(), 'array');
        
        $model = $this->getModel('hello');

        
        if($model->save($data)) {
             
                $this->setMessage('Save successfully');
        } else {
               
                JError::raiseWarning('', 'Save failed<br />'.implode('<br />', $model->getErrors()));
        }

        $this->setRedirect(JRoute::_('index.php?option=com_helloworld&c=hello'));

 

- Modify the files in the folder hello.php models by adding method save.

com_helloworld/models/hello.php

function save($data) 
{       
        $db = JFactory::getDbo();
        $obj = (object) $data;
        try {               
                $db->insertObject('#__helloworld_hello', $obj, 'id');
        } catch (RuntimeException $exc) {            
                $this->setError($exc->getMessage());
                return false;
        }
        return true;
}

 

Display:

 

 

Chapter 11 - Display data from database Component Development