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 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    textarea value process

    Hi

    I wanted to know how to get each line value of a textarea and add <br /> to end of each line before inserting it to database

    lets say textarea is like

    Code:
    line1
    line2
    line3
    i want to read line by line and add a <br /> to end of each line

    line1<br />
    line2<br />
    line3<br />



    any help would be great

    sorry for the language mistakes, hope someone can understand what i mean

    Regards

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    str_replace("\n"'<br />'$_POST['mytextarea']); 
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ive inserted value like

    PHP Code:
    $nfo str_replace("\n"'<br />'$_GET['nfo']); 
    and retrieved it from data base, still text shows like

    line1line2line3

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Not sure why I overcomplicated it, there's a PHP function called nl2br():

    http://php.net/manual/en/function.nl2br.php
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    when i should use nl2br(), when insert into db or when retrieve from db?

    i tried

    PHP Code:
    $nfo nl2br($_GET['nfo']); 
    and insert into db and retrieve it from db but still no luck

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Either when inserting or retrieving should be fine. There's something else wrong here, so show us more of the code.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #7
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i used ajax to submit data, maybe it caused problem

    i tried to insert and retrieve without ajax and nl2br() works perfect

  • #8
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Well, without the code, we can only guess.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #9
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    index.php

    Code:
    <script src="js/submit.js"></script>
    <form action="javascript:insert()" method="post">
    <textarea id="nfo" name="nfo" cols="55" rows="1"></textarea>
    <input name="Submit" type="submit" value="[ Insert Data ]"/>
    </form>
    submit.js looks like this

    PHP Code:
    function createObject() {
    var 
    request_type;
    var 
    browser navigator.appName;
    if(
    browser == "Microsoft Internet Explorer"){
    request_type = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
    request_type = new XMLHttpRequest();
    }
    return 
    request_type;
    }
    var 
    http createObject();
    var 
    nocache 0;

    function 
    insert() {
    document.getElementById('insert_response').innerHTML "<b>Processing...</b>"
    var nfo document.getElementById('nfo').value;
    nocache Math.random();

    http.open('get''insert.php?nfo='+nfo+'&case='+1+'&nocache = '+nocache);
    http.onreadystatechange srvreply;
    http.send(null);
    }

        function 
    srvreply() {
        if(
    http.readyState == 4){
        var 
    response http.responseText;
        
    document.getElementById('cver').valueresponse;
        
    document.getElementById('insert_response').innerHTML ""
            
    }
        } 

    insert.php

    PHP Code:
    if($_GET['case']==1){
        
        if(
    $_GET['nfo']!=""){
            
    $nfo nl2br($_GET['nfo']);  

                            
    $sql "insert into test (info) values ('$nfo')";
            
    mysql_query($sql) or die('Error Inserting Values' mysql_error());
            echo 
    'Successful!';
        }
        else
        {
        echo 
    'Please fill the field!';
        }    


    This is how i retrieve

    PHP Code:
    <?php
    include ("connect.php");

    $id $_GET['id'];

                
    $sql "select * from posts where id='$id'";
                
    $result mysql_query($sql);
                while(
    $dat=mysql_fetch_array($result)){

                echo 
    $dat['info'];    
    }

    ?>
    Last edited by extremed; 12-04-2010 at 02:25 PM.

  • #10
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Unless I'm missing something, it should work. What's in your database? Does it have <br /> in there?
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #11
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    no <br /> in database only text as it is

  • #12
    New Coder
    Join Date
    Jun 2010
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Lamped for your help and time

    finally got it work by replacing below line.

    PHP Code:
    var nfo document.getElementById('nfo').value.replace(/n/g,'<br />'


  •  

    Posting Permissions

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