9

This is a simple question I believe, but can't figure it out yet. I have a text area that after submit goes to a database, and then I echo this text on a page, but here is the problem, say the person writes on the textarea:

Hi Robert,
This is just a test!.
Jason.

And the message goes to the database just like that, but when I echo that, I get:

Hi Robert, This is just a test!. Jason.

This is the form:

<textarea name="newMessage" wrap="hard" cols="30" rows="3"></textarea>

<input type="submit" name="submit" value="Ingresar"> </>

This is the code I use to display the text:

<?php   
    while($row = mysql_fetch_assoc($messages)){
        echo $row['mensaje']."<br/>";
    }
?>

This is what I use to insert the code:

if(isset($_POST['submit'])){        

            $check4LB = $_POST['newMessage'];
            while($letter = mysql_fetch_assoc($check4LB)){
                if($letter=' '){
                $letter='<br/>';
                }
            } /////I know this is not write bu is the idea i thgouht at least

            $query = mysql_query("SELECT (ifnull(max(idRegistro),0) + 1) as id FROM messages");
            $row = mysql_fetch_array($query);
            $idMax = $row['id'];     
            $insertMessage = "INSERT INTO messages(idRegistro, mensaje) VALUES ('".$idMax."','".$letter."')";

             mysql_query($insertMessage) or die(mysql_error());
             echo "<meta http-equiv=Refresh content=\"0 ; url=".$_SERVER['PHP_SELF']."\">"; 
   }
2
  • 1
    user nl2br while inserting $letter ie nl2br($letter) Commented Apr 20, 2011 at 2:17
  • @JapanPro It's better to save data as is and format it only when you want to display it. Commented Apr 20, 2011 at 2:32

3 Answers 3

18

try echo nl2br($row['mensaje']);

Sign up to request clarification or add additional context in comments.

1 Comment

here nl2br($row['stackoverflow_comment_text']); not working
2

Use nl2br() on the output from the database.

Comments

0

Try this

<?php   
    while($row = mysql_fetch_assoc($messages)){
     echo str_replace("\r",'<br/>',$row['mensaje']); 
    }
?>

1 Comment

There are 3 type of newline: \r on mac, \n on linux/unix, \r\n windows. So, it's better to use nl2br() which handle the difference.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.