View Full Version : Textarea - output not showing linebreaks

04-06-2009, 02:20 PM
Hello there
I have a textarea on my website, and when i enter text into it, and if the text has a linebreak in it, it just doesn't read it, and just prints it as 1 line instead of 2 or 3.
Here is the code

<form name="notes"><textarea id="note" cols="40" rows="5"></textarea><br /><input type='button' onclick='UpdateNotes()' value='Update' /></form>

function UpdateNotes(){
var ajaxRequest; // The variable that makes Ajax possible!
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;

// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
var note = document.getElementById('note').value;
var ajaxDisplay1 = document.getElementById('ajaxDiv1');
ajaxDisplay1.innerHTML = note;

var queryString = "?action=update&note=" + note;
ajaxRequest.open("GET", "ajax.php" + queryString, true);
And when i display for example this text
my friend"
It will display it as hellomy friend
Please help

Old Pedant
04-07-2009, 06:40 AM
Nothing to do with Ajax.

Strictly an HTML question.

*ALL* whitespace in HTML output--including many whitespace characters in a row--is displayed in HTML pages as a *single* ordinary space. And, yes, a line break *IS* considered whitespace.

If you want to display text that includes linebreaks in HTML so that the linebreaks show, you can either:
(1) Put the text in between <pre> and </pre> tags.
(2) Use your server side language or client side JavaScript to convert the line breaks into <br /> tags.

04-08-2009, 12:26 AM
For textarea's you have end each line with a CRLF. So, to have an empty line you would simply have just CRLF.

06-19-2010, 10:04 AM
Try This example to solve the problem

echo 'comments'.nl2br($_POST['txtcomments']);

<form method='post' action='textarea.php'>
<textarea name='txtcomments' style='width:300px;height:300px;' >

<input type='submit' value='Click to submit'/>



02-18-2012, 11:42 PM
*edit Sorry, I see that bumping old threads is frowned upon. I'll start a new thread.