views:

672

answers:

4

Hi, i want to have a textarea where I can edit html code directly. After submitting the form the content if the textarea (with html tags) should be saved to a MySQL database. I use PHP to receive the date and save it to the database. My problem is, that the HTML code is not properly sent to PHP. I do not receive the HTML code but just the text. How could I fix this?

my Form looks like this:

<form method="post" enctype="multipart/form-data" action="form.php">
   <textarea name="html_code">
      <a href="link">testlink</a>
   </textarea>
   <input type=submit value="submit"/>
</form>

The form.php should now be able to show the content of the textarea

echo $_POST['html_code'];

shows: testlink

I want: <a href="link">testlink</a>

+1  A: 

Are you echoing it into an HTML page? Because the code will be parsed into an actual link.

View the source of your output page.

Matt
If I view the source of the HTML page I only see "testlink"
dominik
+1  A: 

You're using the wrong encoding type.

Instead of "multipart/form-data", it should be "text/plain".

You don't have to encode the data as Doug says above; but it will be encoded for you when you submit the form, so don't forget to decode before using.

Robert Elwell
I don't think encoding is the problem. PHP detects the encoding automatically and decodes what is needed.
Felix
A: 

Your form should be:

<form method="post" enctype="multipart/form-data" action="form.php">
   <textarea name="html_code">
      &lt;a href=&quot;link&quot;&gt;testlink&lt;/a&gt;
   </textarea>
   <input type=submit value="submit"/>
</form>

(No, it's not messed up. They're called HTML entities)

You can use htmlentities() in PHP to achieve that.

Felix
A: 

Thank you all for your answers. I found the problem. It was Joomla. Joomla removed HTML tags when I got strings via getVar. I had to use the mask option JREQUEST_ALLOWRAW to solve the issue.

JRequest::getVar('html_code', '', 'post' , 'STRING', JREQUEST_ALLOWRAW);
dominik
Hi Dominik, its important to specify in your answer that you are using Joomla etc so people can be aware of the possilbe issues such as this one.
Toby Allen