I write a demo like this:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script type="text/javascript">
window.onload = init;
function init() {
var btn = document.getElementById('submit');
btn.onclick = function() {
var str = document.getElementById('testText').value;
alert(str);
var xmlHttp = new XMLHttpRequest();
var url = 'test.php?str=' + str;
xmlHttp.onreadystatechange = function(){
var str = xmlHttp.responseText;
document.getElementById('testText').value = str.toLocaleUpperCase();
};
xmlHttp.open('GET', url, true);
xmlHttp.send(null);
};
}
</script>
</head>
<body>
<form action="test.php" method="POST">
<textarea name="testText" id="testText" cols="30" rows="10"></textarea>
<input type="button" value="submit" id="submit">
</form>
</body>
</html>
It send a GET to server, and the php just return what it get, then js show it by uppercase(write this because of system ask me to write more detail of code) I write some text in textarea
tag like
write some thing
write other thing
and output is
WRITE SOME THING WRITE OTHER THING
but I want to remain the blank lines, and expect output like this
WRITE SOME THING
WRITE OTHER THING
how should I do?
When you send your data to PHP, you need to convert your text into URL format.
var url = 'test.php?str=' + encodeURIComponent(str);
When you output the PHP into an HTML document, you need to convert your text into HTML.
Most elements do not treat whitespace as significant, so you need to convert the new lines into line break elements or do some other kind of formatting. (In general, you want to use something smarter, like Markdown syntax, but I'll use nl2br for this simplistic example):
<div>
<?php
$data = $_GET['str'];
$safe_data = htmlspecialchars($data);
$formatted_data = nl2br($safe_data);
echo $formatted_data;
?>
</div>
or, if you are outputting into an element with significant whitespace
<textarea>
<?php
$data = $_GET['str'];
$safe_data = htmlspecialchars($data);
echo $safe_data;
?>
</textarea>
You can use the NewLine character entity, which can be expressed using any of the following:

 

Like so:
write some thing
write other thing
...or:
write some thing
write other thing
...or:
write some thing write other thing
<textarea>write some thing
write other thing</textarea>
<textarea>write some thing
write other thing</textarea>
<textarea>write some thing write other thing</textarea>
</div>