Showing what is submitted in a textarea exactly as the user enters

Hi All

I think I am having a brain fart on this as I have not worked on this sort of thing for a long time and it used to work… I think something may have changed.

I am using PHP 7.4.

I have a form with a textarea:

<div class="form-group"><textarea class="form-control" rows="10" name="ticketcomments" placeholder="Your question or comments" required></textarea></div>

When the user submits the form I have htmlspecialchars in place:

$ticketcomments = htmlspecialchars($ticketcomments);

When I output what the user added all the formatting is gone…

EXAMPLE IN FORM

This is my first line.

This is my second line.

WHAT I GET

This is my first line. This is my second line.

How can I get my output to be the same as what the user inputs.

What am I missing?

mrmbarnes

you are missing either

  1. to hit ctrl+u to show the sourcecode, as users dont insert html into a form, but plain text

  2. or to transform the plain text input into the html context, where linebreaks are not \n but < br/ >, nl2br() can do that for you.

Off Topic

@mrmbarnes: when you post code on the forums, you need to format it so it will display correctly.

You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

https://www.php.net/nl2br

Ah, this can be an annoying little problem. Try the below code in your PHP processor for that specific field, this may not be exact depending on your current PHP but should look something like below:

$details = $_REQUEST['ticketcomments']; 
$details = nl2br($details);

get_magic_quotes_gpc is deprecated @formcodes

1 Like

Wow, made me reference the processor where I grabbed the snippet from in my files…that was from the pre 5.3 era. Good callout, I will change for the OP.

That did the trick… thanks everyone

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.