Jump to content

Echo'ing HTML


stevegc

Recommended Posts

Hi everyone,

 

First time post for me :)  I am quite new to PHP so excuse the beginner question but I can't find and answer for it.

 

I have a backend application that allows an admin to update the frontend homepage with a WYSIWYG html editor (Xinha).  I store the html into mysql and retrieve the data for display on the front end homepage.  The problem is the html is not rendering and displaying with the tags as text.

 

When saving the data to the db I use htmlentities($data) and when retrieving I used html_entity_decode($data) before passing it to the template to be rendered.  I am using a simple <?php echo $data ?> to display the data.

 

On the same page I have <?php $testingHTML = "<p>This is some text with a <b>bold</b> word in it</p>" ?> followed down the page by an <?php echo $testingHTML ?> and that renders perfectly.  So I must be doing something wrong?

 

Thank you in advance and I hope that makes sense.

 

Steve

Link to comment
Share on other sites

There's no need to call htmlentities or html_entity_decode at all. Take that out, and you're fine.

 

This can be dangerous though. You should only allow trusted users to use this form, to prevent someone from injecting malicious JavaScript or HTML into your page.

 

If you don't trust the people entering data into the form, then use an implementation of BBCode or something similar.

Link to comment
Share on other sites

There's no need to call htmlentities or html_entity_decode at all. Take that out, and you're fine.

 

This can be dangerous though. You should only allow trusted users to use this form, to prevent someone from injecting malicious JavaScript or HTML into your page.

 

If you don't trust the people entering data into the form, then use an implementation of BBCode or something similar.

 

Thanks for the reply xyph.  I tried it without the htmlentities and html_entities_decode but got the same results.

The only people with access to this will actually be my parents so it should be quite safe in terms of malicious code :)

Link to comment
Share on other sites

Yeah I have checked that.  Here is exactly what is in the DB.

 

<p> <strong>Here is some bold text</strong></p>

  <p><strong></strong>Here is some normal text about nothing just to fill in space and test things out<br /> </p>

 

[attachment deleted by admin]

Link to comment
Share on other sites

Ok Sorry about that.  Just thought that gave a better perspective.

 

The methods for inserting and retrieving data


public function updateHomePageData($data) {

	$sql = "update general_data set data_value = '" . $data . "' where data_key = 'HMPGD'"; 
	//htmlspecialchars($data) . "' where data_key = 'HMPGD'";
	$con = Doctrine_Manager::getInstance()->connection();
	$con->execute($sql);
}

public function getHomePageData() {
	$homePageData = "";

	$sql = "select * from general_data where data_key = 'HMPGD'";
	$con = Doctrine_Manager::getInstance()->connection();
	$resultSet = $con->execute($sql);

	foreach ($resultSet as $result) {
		$homePageData = $result['data_value'];
	}
	return $homePageData;
}

 

The function that passes data to the template

        public function executeShow(sfWebRequest $request)
{
	$generalData = new GeneralData(); 
	$homePageData = "";
	$homePageData = $generalData->getHomePageData();
	$this->homePageData = $homePageData;
}

 

the template

<div>
<?php echo $homePageData ?>
</div>

<?php 
$testingHTML = "<p>This is some <b>bold</b> text</p>";
?>

<?php echo $testingHTML ?>

 

 

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.