Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-06-2009, 01:20 PM   PM User | #1
kenno69
New to the CF scene

 
Join Date: Feb 2009
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
kenno69 is an unknown quantity at this point
Textarea - output not showing linebreaks

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
Code:
<form name="notes"><textarea id="note" cols="40" rows="5"></textarea><br /><input type='button' onclick='UpdateNotes()' value='Update' /></form>
Code:
function UpdateNotes(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				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);
	ajaxRequest.send(null); 
}
And when i display for example this text
"hello
my friend"
It will display it as hellomy friend
Please help
kenno69 is offline   Reply With Quote
Old 04-07-2009, 05:40 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
or
(2) Use your server side language or client side JavaScript to convert the line breaks into <br /> tags.
Old Pedant is offline   Reply With Quote
Old 04-07-2009, 11:26 PM   PM User | #3
rfresh
Regular Coder

 
Join Date: Jun 2007
Location: Los Angeles
Posts: 545
Thanks: 81
Thanked 5 Times in 5 Posts
rfresh is an unknown quantity at this point
For textarea's you have end each line with a CRLF. So, to have an empty line you would simply have just CRLF.
__________________
RalphF
Business Text Messaging Services
https://www.MobileTextingService.com
rfresh is offline   Reply With Quote
Old 06-19-2010, 09:04 AM   PM User | #4
Dineshphp
New to the CF scene

 
Join Date: Jun 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Dineshphp is an unknown quantity at this point
nl2br() we can solve this problem

Try This example to solve the problem


<?php
if($_POST['txtcomments'])
{
echo 'comments'.nl2br($_POST['txtcomments']);
}
else
{
?>

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

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

<?php

}
?>

Last edited by Dineshphp; 06-19-2010 at 09:06 AM.. Reason: alignment
Dineshphp is offline   Reply With Quote
Old 02-18-2012, 10:42 PM   PM User | #5
uparmored
New to the CF scene

 
Join Date: Feb 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
uparmored is an unknown quantity at this point
*edit Sorry, I see that bumping old threads is frowned upon. I'll start a new thread.

Last edited by uparmored; 02-18-2012 at 10:45 PM..
uparmored is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, teaxtarea

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:20 AM.


Advertisement
Log in to turn off these ads.