From this answer it appears that data sent via the HTTP POST method must be present in form elements. I want to be able to send the contents of a <textarea>
element using POST, but I imagined that <textarea>
can't be part of a form. I could use javascript to copy the textarea contents to a (preferably hidden) form's text input field and then post. But that way the line breaks are lost. Short of inserting special chars to markup the line breaks, is there another method to preserve line breaks in POST data?
Asked
Active
Viewed 1,603 times
-1

danbae
- 563
- 2
- 8
- 22
-
2Actually – Yevhenii Shashkov Oct 03 '17 at 09:12
-
can you share your code? – Super User Oct 03 '17 at 09:13
-
Sorry – I read somewhere an explanation of ` – danbae Oct 03 '17 at 09:52
2 Answers
0
Actually <textarea>
can be a part of a <form>
:
<form action="/action_page.php" id="usrform">
Name: <input type="text" name="usrname">
<input type="submit">
</form>
<textarea name="comment" form="usrform">Enter text here...</textarea>

Yevhenii Shashkov
- 464
- 1
- 8
- 19
-
-
@PeterB — It has a `form` attribute instead (although that is an odd choice) – Quentin Oct 03 '17 at 09:25
-
Thanks, I see now, never heard of it before. Still behind on HTML5. A quick search for it also revealed where the code sample came from: https://www.w3schools.com/tags/att_textarea_form.asp – Peter B Oct 03 '17 at 09:28
-
Works fine. Sorry about my confusion. I think I tried a lot of variants but missed this one. Thank you. – danbae Oct 03 '17 at 09:55
0
Textarea values are easily included in form submissions as long as it has a 'name' prop.
You can also include the value afterwards by serializing the form data before submission. This is just a string of form data that you can append to.
Line breaks are always preserved in textarea values, you just need to make sure to render them when that value is displayed, as seen here.

Chase
- 3,009
- 3
- 17
- 23