Making objectHTMLSafe work

Have a programming question regarding your component, plug-in, extension or core hacks? Have an interesting tidbit, FAQ or programming tip you’d like to share? This is the place for you.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Post Reply
Plaidfox
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Feb 08, 2008 5:17 pm

Making objectHTMLSafe work

Post by Plaidfox » Mon Mar 03, 2008 9:58 pm

Could use some help with my coding. I'm trying to get a textarea to save html code to my mysql database. Can anyone tell me how to set this up?

Thanks!

Plaidfox
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Feb 08, 2008 5:17 pm

Re: Making objectHTMLSafe work

Post by Plaidfox » Mon Mar 03, 2008 10:33 pm

Here's my code so far, if it helps:

Code: Select all

<?php defined('_JEXEC') or die('Restricted access');?>

<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col100">
	<fieldset class="adminform">
		<legend><?php echo JText::_( 'Details' ); ?></legend>

		<table class="admintable">
		<tr>
			<td width="100" align="right" class="key">
				<label for="title">
					<?php echo JText::_( 'Title' ); ?>:
				</label>
			</td>
			<td>
				<input class="text_area" type="text" name="title" id="title" size="32" maxlength="250" value="<?php echo $this->recipe->title;?>" />
			</td>
		</tr>
        <tr>
 		<td width="100" align="right" class="key">
        	<label for="ingredients">
            	<?php echo JText::_( 'Ingredients' ); ?>:
            </label>
        </td>
        <td>
        	<textarea class="inputbox" cols="70" rows="10" name="ingredients" id="ingredients"><?php echo $this->recipe->ingredients;?></textarea>
        </td>
    </tr>
        </table>
        <table class="adminform">
        <tr>
        <td width="100" align="right" class="key">
        		<label for="directions">
                	<?php echo JText::_( 'Directions' ); ?>:
                </label>
        </td>
        <td>
						<textarea id="directions" name="directions" cols="75" rows="20" style="width:100%; height:550px;" class="mce_editable="><?php echo $this->recipe->directions;?></textarea>

<div id="editor-xtd-buttons">
<div class="button2-left"><div class="image"><a class="modal-button" title="Image" href="http://www.kernandlead.com/~russell/administrator/index.php?option=com_media&view=images&tmpl=component&e_name=text"  rel="{handler: 'iframe', size: {x: 570, y: 400}}">Image</a></div></div>
<div class="button2-left"><div class="pagebreak"><a class="modal-button" title="Pagebreak" href="http://www.kernandlead.com/~russell/administrator/index.php?option=com_content&task=ins_pagebreak&tmpl=component&e_name=text"  rel="{handler: 'iframe', size: {x: 400, y: 85}}">Pagebreak</a></div></div>
<div class="button2-left"><div class="readmore"><a  title="Read more..." href="http://www.kernandlead.com/~russell/administrator/#" onclick="insertReadmore('text');return false;" rel="">Read more...</a></div></div>
</div>
					</td>
                    </tr>
	</table>
	</fieldset>
</div>
<div class="clr"></div>

<input type="hidden" name="option" value="com_recipe" />
<input type="hidden" name="id" value="<?php echo $this->recipe->id; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="recipe" /> 
</form>
I want to allow the user to put html code in that textarea section.

Bodom78
Joomla! Apprentice
Joomla! Apprentice
Posts: 19
Joined: Fri Nov 04, 2005 9:23 am

Re: Making objectHTMLSafe work

Post by Bodom78 » Wed Mar 05, 2008 8:49 am

Just before you store your data, intercept the text area content like so:

Code: Select all

$data = JRequest::get( 'post' );
$data['ingredients'] = JRequest::getVar( 'ingredients, '', 'post', 'string', JREQUEST_ALLOWRAW );

// Bind the form fields to the table
if (!$row->bind($data)) {
	$this->setError($this->_db->getErrorMsg());
	return false;
}

// Make sure the record is valid
if (!$row->check()) {
	$this->setError($this->_db->getErrorMsg());
	return false;
}
		
// Store the content to the database
if (!$row->store()) {
	$this->setError( $row->getErrorMsg() );
	return false;
}

return true;
Maybe theres another way to do it, but this has worked for me.

Plaidfox
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Feb 08, 2008 5:17 pm

Re: Making objectHTMLSafe work

Post by Plaidfox » Thu Mar 06, 2008 5:46 pm

So, should the code you gave me be put on the form.php page or on some other page? I based my component off the hello_world component shown to us by joomla.

Plaidfox
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Feb 08, 2008 5:17 pm

Re: Making objectHTMLSafe work

Post by Plaidfox » Thu Mar 06, 2008 8:38 pm

Well, I tried putting your code into my controllers/recipe.php, but I keep getting this error: Fatal error: Call to a member function on a non-object in /home/russell/public_html/administrator/components/com_recipe/controllers/recipe.php on line 51

Here's my code:

Code: Select all

/**
	 * save a record (and redirect to main page)
	 * @return void
	 */
	function save()
	{
		$data = JRequest::get( 'post' );
		$data['directions'] = JRequest::getVar( 'directions', '', 'post', 'string', JREQUEST_ALLOWRAW );

// Bind the form fields to the table
if (!$row->bind($data)) {
   $this->setError($this->_db->getErrorMsg());
   return false;
}

// Make sure the record is valid
if (!$row->check()) {
   $this->setError($this->_db->getErrorMsg());
   return false;
}
      
// Store the content to the database
if (!$row->store()) {
   $this->setError( $row->getErrorMsg() );
   return false;
}

return true;
		
		$model = $this->getModel('recipe');

		if ($model->store($post)) {
			$msg = JText::_( 'Recipe Saved!' );
		} else {
			$msg = JText::_( 'Error Saving Recipe' );
		}

		// Check the table in so it can be edited.... we are done with it anyway
		$link = 'index.php?option=com_recipe';
		$this->setRedirect($link, $msg);
	}

Plaidfox
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Fri Feb 08, 2008 5:17 pm

Re: Making objectHTMLSafe work

Post by Plaidfox » Fri Mar 07, 2008 3:32 pm

Oh, nevermind, figured it out. For future readers, all I did was paste:

Code: Select all

$data['directions'] = JRequest::getVar( 'directions', '', 'post', 'string', JREQUEST_ALLOWRAW );
into my models/recipe.php file right where Bodom78 was saying it went and now it works. :D Thanks for the help!


Post Reply