Follow @BPSPro

PHP How to Display HTML Code in a Form – Display HTML code instead of output

1 Comment RSS Site Feed Author: AITpro Admin
Published: March 11, 2011
Updated: March 20, 2011

To display HTML code in a form instead of processing or outputting the HTML code, or to be more technically correct the HTML special characters, you would use the PHP htmlspecialchars( ) string function.  If you want to display all  HTML character entities then y0u would use the htmlentities( ) string function instead

How to display HTML code in a form and not process or output the HTML code:

In this form example the form has a text field where YouTube embed code is allowed to be entered to display a YouTube video in a directory listing submission.  The HTML code needs to be displayed instead of allowing PHP to process the HTML code becaue the submit form is re-editable.  What happens if you don’t display the actual HTML code instead of allowing the code to be processed or ouputted is that the YouTube embed code will display the video in the submit form if a person is re-editing their directory listing submission.  So the goal is to have the actual YouTube HTML embed code be displayed instead of allowing the HTML code to be processed or outputted and display the actual Youtube video on re-edit of the submit form.  This is a very simple thing to accomplish using the PHP htmlspecialchars( ) string function.

Example:

   if(isset($_POST['action']) && !isset($data) ){ echo $_POST['Dir']['youtube_url_55'];
} elseif (isset($data)){ echo htmlspecialchars($data['youtube_url_55'][0]); } ?>" />

So basically what is happening above is that the form is being checked to see if the post action is selected and if there is data entered into the text field (that portion of the form code that contains the actual text field code is not displayed in this example) and if no data is entered yet then the YouTube embed code that the form submitter enters into the text field will be echoed when the form script is processed.  Then the elseif will check to see if data already exists and if it does it will echo that data that already exists.  So here is where htmlspecialchars is important because if the user is re-editing the form and htmlspecialchars is not used then the form will display the actual YouTube video not the actual YouTube HTML embed code, which is what you want for a number of reasons.  One really obvious one would be if they wanted to change the size dimensions of the YouTube video displayed in the directory listing.  They could simple just edit the HTML code and change those dimensions without having to edit the YouTube HTML embed code outside of the form and then have to copy and paste that HTML code to the form again. 

The HTML code is displayed in the submission form on re-edit.  If the htmlspecialchars string function was not used here the user would see the actual YouTube video embedded into the form.

Click Image to Enlarge
PHP Display html Code in Form not Output


PHP code Reference Link
PHP htmlspecialchars string function

Skip to toolbar