Joomla - Component: Saving Data to Database


After build form, we need code for data processing. When the data in form is filled out and the admin clicks the Save button, we need to save the information into database.

  1. Create saveHello() in admin.hello.php:
    function saveHello(){
    	global $mainframe;
    	$row =& JTable::getInstance('hello', 'Table');
    	if(!$row->bind(JRequest::get('post')))
    	{
    		JError::raiseError(500, $row->getError() );
    	}
    	$row->message = JRequest::getVar( 'message', '','post', 'string', JREQUEST_ALLOWRAW );
    	
    	if(!$row->store()){
    		JError::raiseError(500, $row->getError() );
    	}
    	$mainframe->redirect('index.php?option=com_hello', 'Message Saved');
    }
    
  2. Add a case to save data, below to the switch():
    	case 'save';		
    		saveHello();
    		break;
    
  3. Add a line code to Set the table directory before switch():
    JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS. 
    'com_hello'.DS.'tables');
    
The complete code:
<?php
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// to include admin.hello.html.php
require_once( JApplicationHelper::getPath( 'admin_html' ) );
// Set the table directory
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.
'com_hello'.DS.'tables');

$task	= JRequest::getCmd('task');

// checks the $task variable and choose an appropiate function
switch($task){
	case 'add':
		addHello();
		break;
		
	case 'cancel';		
		showHello();
		break;
		
	case 'save';		
		saveHello();
		break;
		
	default:
		showHello();
		break;

}

function addHello(){
	$lists['published'] = JHTML::_('select.booleanlist', 'published' , 'class="inputbox"', $row->published);

// display function
	HTML_contact::addHello($lists);
}

function showHello(){
// display function
	HTML_contact::showHello();
}

function saveHello(){
	global $mainframe;
	$row =& JTable::getInstance('hello', 'Table');
	if(!$row->bind(JRequest::get('post')))
	{
		JError::raiseError(500, $row->getError() );
	}
	$row->message = JRequest::getVar( 'message', '','post', 'string', JREQUEST_ALLOWRAW );
	
	if(!$row->store()){
		JError::raiseError(500, $row->getError() );
	}
	$mainframe->redirect('index.php?option=com_hello', 'Message Saved');
}
?>

Point your browser to http://localhost/joomla/administrator/index.php?option=com_hello&task=add. Enter a data to the form. Click save button on the top right. If success, you will get a message like following images:

Joomla: Data Saved


Series this article:
Joomla - Component: Create Hello World Component on Front End
Joomla - Component: Create Hello World Component on Back End
Joomla - Component: Registering Your Component in database
Joomla - Component: Create Component Link for Front End
Joomla - Component: Creating Toolbars
Joomla - Component: Available Toolbar Buttons
Joomla - Component: Creating the Database Table
Joomla - Component: Creating Form Input Data at Back End
Joomla - Component: Saving Data to Database
Joomla - Component: Creating a List Screen
Joomla - Component: Creating Advance List Screen
Joomla - Component: Editing Data
Joomla - Component: Deleting Data
Joomla - Component: Updating publish state
Joomla - Component: Hello From Database at Front End
Joomla - Component: a Hello page
Joomla - Component: Generating Friendly Links
Joomla - Component: Adding Comment ( Create Table )
Joomla - Component: Adding Comment (Create Form)
Joomla - Component: Adding Comment ( Insert Data )
Joomla - Component: Adding Comment ( Displaying Comments )
Joomla: Fast Road to Understand Component Programming

Bookmark and Share Tag: joomla, cms, Joomla Component Category: PHP Framework Post : November 05th 2007 Read: 99,342

advertisements


blog comments powered by Disqus