Sharon
Sharon

Reputation: 3919

How can I POST html?

I need to send a form to PHP, but one of the fields contains HTML.

When it's in the form it's fine, and will show:

<input id="addNote" value="<div class="line">This is my line</div>"/>

However, when I POST it to the server, the HTML tags have been stripped out, so it comes through as 'This is my line'. What do I need to do to make sure the tags don't get stripped out?

Thanks!

Upvotes: 0

Views: 98

Answers (3)

Vikas Naranje
Vikas Naranje

Reputation: 2412

in your .php file use this -

echo htmlentities($_POST['addNote']);

and in your html file use single quote.

<input id="addNote" name="addNote" value="<div class='line'>This is my line</div>"/>

Upvotes: 0

Dennis
Dennis

Reputation: 14477

<input id="addNote" value="<div class="line">This is my line</div>"/> does not work at all!

Since you're using " to limit the value of the input field, you have two choices:

  1. Change the " character to ': This is my line'/>

  2. Substitute the quotes (") inside the value for &quot;: This is my line"/>

However, none of this matters if your PHP script deletes HTML tags...

Upvotes: 0

Marc B
Marc B

Reputation: 360882

When embedding html-in-html, you should encode the HTML metacharacters so they can't be mis-interpreted:

<input id="addNote" value="&lt;div class=&quot;line&quot;&gt;This is my line&lt;/div&gt;" />

This is especially true with " characters, as they'll break the form for the parser. e.g.

<input ... value="<div class="line" ....  />
                             ^---

The indicated quote will be translated as ENDING the value= portion, and line" being the start of some other non-standard/unknown tag attribute.

Upvotes: 3

Related Questions