Friday, July 31, 2009

How do you get HTML tags to display in a form field without rendering them?

I have a PHP page that's pulling in HTML text with tags characters included into a form field that I want to appear as HTML for editability and then save it back via PHP the way it was formatted. Unfortunately, when it loads into the form field it shows words in bold instead of having the tags around them, etc.





Do you have to format them differently in PHP before adding it to the form field? or is there a setting for form fields to keep them from applying the HTML code?

How do you get HTML tags to display in a form field without rendering them?
Yeah, these guys are right, but if you don't want to bother to create the code, that is not interpreted as html, you could let php do it.





Just type your html into a variable, and use the htmlspecialchars function:





$myHtml="%26lt;b%26gt; %26lt;i%26gt; %26lt;font color='red' %26gt;test %26lt;/font%26gt; %26lt;/i%26gt; %26lt;/b%26gt;";


echo htmlspecialchars( $myHtml );





This is the documentation of htmlspecialchars:


http://hu.php.net/manual/en/function.htm...





Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.





This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book application. The optional second argument, quote_style , tells the function what to do with single and double quote characters. The default mode, ENT_COMPAT, is the backwards compatible mode which only translates the double-quote character and leaves the single-quote untranslated. If ENT_QUOTES is set, both single and double quotes are translated and if ENT_NOQUOTES is set neither single nor double quotes are translated.
Reply:The php part have nothing to do here, it's irrelevant, what your browser show based on the html output of your php code.





Try to replace the special characters with their code (like '%26lt;' to '%26lt;' and '%26gt;' to '%26amp;rt' etc. see full explanation at http://www.w3schools.com/html/html_entit...





You can use this converter:


http://www.dustinsenos.com/character_enc...





You can also check if your html/xhtml is valid here - http://validator.w3.org/.
Reply:One way is to use the escape code equivalent...the browser will convert them to the characters but won't use the html tag formatting





http://www.dragonwinds.com/resources/htm...


No comments:

Post a Comment