Post from custom form - please help

For Joomla! 2.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Note: All 1.6, 1.7 and 3.5 releases have reached end of life and should be updated to 3.x.

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Locked
jm1968a
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Dec 16, 2011 8:50 pm

Post from custom form - please help

Post by jm1968a » Sat Feb 18, 2012 2:52 am

Hi All,I need to understand this, hopefully someone can help.

I'm working on a component and need to add form data to a table. I have soe experience with php, but am new to Joomla!

I have a view (myart/view/add/tmpl/defaut.php) which has:

Code: Select all

<form id="form1" name="form1" method="post" action="">

    <tr>
      <td><label>Art Title: </label></td>
      <td><input type="text" name="title" id="title" /></td>
    </tr>
    <tr>
      <td>SKU:</td>
      <td><input type="text" name="sku" id="sku" /></td>
    </tr>
    <tr>
      <td>Price:</td>
      <td><input type="text" name="price" id="price" /></td>
I have a model(myart/model/add.php) which has:

Code: Select all

<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.modellist');
		// get email variables
		$title		 		= $_POST['title'];
		$price 		 		= $_POST['price'];	
		$sku				= $_POST['sku'];
	
class myartModelAdd extends JModelList
{
	public function getListQuery()
	{
		$user =& JFactory::getUser();
		$user_id = $user->id;
	
		$db =& JFactory::getDBO();

		$query = "INSERT INTO #__myart (user_id, title, price, sku,)
			VALUES ($user_id, $title, $price, $sku,)";
		$db->setQuery( $query );
		$new_image = $db->loadResult();
		return $new_image;
	}
}
Can someone shoe me how to code this correctly, I’m kinda lost. Also what should I put as an action for the form?

Jason

User avatar
mtrivino
Joomla! Apprentice
Joomla! Apprentice
Posts: 32
Joined: Wed Aug 19, 2009 9:47 pm
Location: Bogotá, Colombia
Contact:

Re: Post from custom form - please help

Post by mtrivino » Tue Feb 21, 2012 1:18 am

Hi,

o.k., you're doing it the hard way (you won't benefit from abstraction if you hard code...). Joomla has a class called JTable and it is meant to be used for things like this. Here's a quick overview of what you should do:

1) create a JTable extension class having attributes matching those from your table.
e.g.
table:

Code: Select all

CREATE TABLE IF NOT EXISTS `#__smart_pictures`
(
`id` int(11) NOT NULL auto_increment,
`path` varchar(255),
`path_thumbnail` varchar(255),
`name` varchar(255),
`description` varchar(255),
`keywords` varchar(255),
`related_products` varchar(255),
PRIMARY KEY  (`id`)
);
and JTable, located at admin/tables/pictures.php:

Code: Select all

<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla table library
jimport('joomla.database.table');
 
/**
 *  Table class
 */
class SmartTablePictures extends JTable
{
	/*primary key*/
	var $id=null;
	var $path=null;
	var $path_thumbnail=null;
	var $name=null;
	var $description=null;
	var $keywords=null;
	var $related_products=null;
	
	/**
	 * Constructor
	 *
	 * @param object Database connector object
	 */
	function __construct(&$db) 
	{
		parent::__construct('#__smart_pictures', 'id', $db);
	}
}
2) it's "better" if you place methods in controllers, so I would suggest you move the method to site/controller.php
Example:

Code: Select all

public function save_picture(){
		//get id number
		$id = JRequest::getVar('id', 0);
		//variable for error counting
		$error_count=0;
		//create a database object instance
		$type ='Pictures'; 
		$prefix = 'SmartTable'; 
		$config = array();
		$row = JTable::getInstance($type, $prefix, $config);
		if($id==null || $id='') {
			//create picture
			$row->reset();
		}		
		else{
			//edit picture
			$row->load($id);
		}
		//associate the database object to the form
		if (!$row->bind(JRequest::get('post'))) {
			$error_count=$error_count+1;
		}
		if($error_count==0){
			if (!$row->store()) {
				JError::raiseWarning(100, $message1);
			}
			else {
				$backlink = JRoute::_('index.php?option=' . $option . '&view=pictures' );

$this->setRedirect($backlink,JText::_('COM_SMART_PICTURE_ADDED'));
			}
		}
	}
This is just a method in your class extension, not the whole file.
3) in your form assign the method's name to the task atribute, i.e., you should have something like this:

Code: Select all

<input type="hidden" name="id" value="<?php echo $this->item->id; ?>" />
	<input type="hidden" name="task" value="save_picture" />
	<input type="hidden" name="option" value="<?php echo (JRequest::getCmd('option')) ?>" />
	<input type="submit" class="button" id="button" value="Aceptar"/>
Joomla tries to match the values in your table to those in the form, so make sure you use the same names. Refer to the MVC tutorials in docs.joomla.org/developers.

It might seem as overkill, but it's not that hard once you get used it.

Regards.

jm1968a
Joomla! Apprentice
Joomla! Apprentice
Posts: 5
Joined: Fri Dec 16, 2011 8:50 pm

Re: Post from custom form - please help

Post by jm1968a » Thu Feb 23, 2012 10:05 pm

thanks so much for your reply. I will work on this...


Locked

Return to “Joomla! 2.5 Coding”