Form submit losing textarea CR LF characters

I have a php script that is doing a form submit via javascript. I’m not sure if I should be posting this in the PHP or the Javascript section but I think this isn’t a PHP issue.

I have a textarea element and when I submit (via $_POST), I am losing all my CR LF characters. I’ve searched all morning and read about similar problems but everything I’ve tried doesn’t work.

I must be missing something really simple perhaps?

Here is my main script. I type in the following then I click the submit button:


The script here. PLEASE note I had to remove the first character in each line (the ‘<’ char) in order to get the code to display here: Fixed - TechnoBear

<script src=""></script>
<script type="text/javascript">
	$("#idsave2").click(function() {
		document.forms[0].post_emailComments.value = document.getElementsByName("name_emailComments")[0].value;//textarea
<form NAME="citwb2" ACTION="citwb_dispatcher.php" METHOD="post">
	input hidden type="text" name="post_emailComments" value="">
	textarea rows="6" name="name_emailComments" style="width:20%;"></textarea>
	button type="button" id="idsave2" >Submit</button>

and here is my PHP dispatcher script that is being called:

$emailComments = $_POST['post_emailComments'];
echo strlen($emailComments)."<br>";
echo $emailComments."<br>";
for ( $pos=0; $pos < strlen($emailComments); $pos ++ ) {
 $byte = substr($emailComments, $pos, 1);
 echo $byte."=".ord($byte)."<br>";

When I click submit I see this (below) on the web page which clearly does not have any CR or LF characters:

I just cannot figure out what I am missing?

When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

1 Like

There was information about posting code somewhere in the FAQ.

Ahh, here’s the thread.

1 Like

To see the actual contents of the $_POST variable I use the following

echo var_dump($_POST);

This will display what is actually being sent. It can sometimes surprise what is sent.