Skip to content
Advertisement

How to preserve newlines in a textarea when passing by POST?

In my web page, I am passing the value of a textarea to PHP via the POST Method. That value is supposed to be written into a file, but in the $_POST key which contains the value, it has no line breaks, so when it is written to the file, there are no line breaks (as if some one replaced n with nothing).

Is there some PHP/JS function that replaces JS textarea newlines into PHP newlines that work in files?

Edit:

My code is supposed to be a file editor. Here it is:

JavaScript

Advertisement

Answer

I see that the purpose of your javascript code is only to fill in any input values. This can be done by utilizing <form> directly. Most likely, copying your data into a <input tye="text"> (text is the default type) will mangle your newlines.

Something like this should be equivalent and without the presented issue:

JavaScript

Also as mentioned, you will probably need to format the newlines for html when displaying.


When needing to pass the same or hidden data you can use <input type="hidden" name=".." value="..">. This is used to pass the filename. The desired action is passed with the button press. The name of button you press will be passed in the request.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement