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!
Making objectHTMLSafe work
Moderators: tjay, seadap, Rogue4ngel, matthewhayashida
Re: Making objectHTMLSafe work
Here's my code so far, if it helps:
I want to allow the user to put html code in that textarea section.
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>
Re: Making objectHTMLSafe work
Just before you store your data, intercept the text area content like so:
Maybe theres another way to do it, but this has worked for me.
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;
Re: Making objectHTMLSafe work
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.
Re: Making objectHTMLSafe work
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:
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);
}
Re: Making objectHTMLSafe work
Oh, nevermind, figured it out. For future readers, all I did was paste: into my models/recipe.php file right where Bodom78 was saying it went and now it works. Thanks for the help!
Code: Select all
$data['directions'] = JRequest::getVar( 'directions', '', 'post', 'string', JREQUEST_ALLOWRAW );