Search code examples
phphtmlgettextarea

Refreshing form not preserving textarea line breaks


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!


Solution

  • 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>