-2

I have a simple form as shown below, with a few inputs, with only one textarea input shown below:

<form class="box" action = "?" method = "GET" enctype = "multipart/form-data">

<textarea id = "query" name = "query" style="width:810px;height:150px;border:solid 1px #737373;white-space: pre-line;"></textarea>

<script type="text/javascript">
document.getElementById('query').value = "<?php echo $_GET['query'];?>";
</script>

</form>

My form gets refreshed when users select different inputs, using:

onchange = "this.form.submit()"

When my form is refreshed, I would like to keep all the inputs so far, which is why I have the following script tags in my form shown above:

<script type="text/javascript">
document.getElementById('query').value = "<?php echo $_GET['query'];?>";
</script>

This does a good job and keeps all the text which is in a single line, but it does not work when the textarea has more than one line, and it displays nothing. I even tried changing the css for the text area to:

white-space: pre-wrap;

but it did not seem to work!

How can i refresh my form and keep the text area text to handle new lines?

Thanks!

Aliyan Haq
  • 302
  • 1
  • 7
  • Does this answer your question? [How do I preserve line breaks when getting text from a textarea?](https://stackoverflow.com/questions/40417527/how-do-i-preserve-line-breaks-when-getting-text-from-a-textarea) – FluffyKitten Aug 18 '20 at 20:37
  • well I assume it will have line breaks in it and line breaks will cause issue with the JavaScript variable. Why are you even using JavaScript to set it, why is PHP not setting it directly in the textarea?? – epascarello Aug 18 '20 at 20:37
  • @espascarello users will be filling in the the textarea input, i just want the text to stay in the textarea input, as users select other inputs which will cause the form to submit – Aliyan Haq Aug 18 '20 at 20:40
  • @FlufflyKitten that is a bit different, I want to preserve the new lines within the textarea input rather than elsewhere, I tried their solution and it still did not work. Thanks anyways! – Aliyan Haq Aug 18 '20 at 20:41
  • _“and it displays nothing”_ - apparently you neglected to check the browser console, because surely there would have been a message about an unterminated string literal in there. – CBroe Aug 19 '20 at 08:16

1 Answers1

0

You don't need javascript code! You can change your code to this:

<form class="box" action = "?" method = "GET" enctype = "multipart/form-data">

<textarea id = "query" name = "query" style="width:810px;height:150px;border:solid 1px #737373;white-space: pre-line;"><?php echo $_GET['query']; ?></textarea>

</form>
  • Your welcome dear. I updated my answer and I deleted deleteed nl2br function, So you don't need str_replace function ;) – Mostafa Kalantari Fard Aug 18 '20 at 21:04
  • @AliyanHaq wrapping this in `nl2br` is how you are _creating_ those `
    ` in the first place. With `str_replace("
    ", "", nl2br($_GET['query']))` you are doing one thing, only to then immediately reverse it. Makes zero sense.
    – CBroe Aug 19 '20 at 08:18