Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    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

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •