Take the 2-minute tour ×

I have created a form

<form name="form1" method="post">
    <textarea rows="5" cols="20" name="ta1"></textarea>
    <textarea rows="5" cols="20"><?php echo $_POST['ta1']; ?></textarea>

And a PHP code

    if ($_POST['submit']) {
        $llo = $_POST['ta1'];
        $con1 = mysql_connect("localhost", "FreeUser", "123456");
        mysql_select_db("database", $con1);
        mysql_query("insert into test (ttta) values ('$llo')");
        $q1 = mysql_query("select * from test");
        $res = mysql_fetch_array($q1);
        echo $res['ttta'];

Database table test

id Auto_Increment
ttta mediumtext 500

only two fields in table

Now when i type something in textarea as formatted below

Web Developer
XYZ Company
New York

When i press the submit button, same content with same formatting structure in stored / displayed in database table / textbox

But when i echo this value its displaying in single line without any format. How can we achived this.

And i have added an image for better understanding

enter image description here

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted


echo nl2br($res['ttta']);

That will convert \n into the HTML tag <br />

In HTML, visible line brakes requires the use of the HTML tag <br />, but in your PHP, the stored data is "Rafee\nWeb Developer\nXYZ Company\nNew York\n", so you need the function to turn "\n" into "<br />".

Reference: http://php.net/manual/en/function.nl2br.php

share|improve this answer
+1. Great answer. –  Radu Murzea Jun 20 '12 at 7:56
Thanks, need more help on this.. Is there anything.. that will read each and every line of echo nl2br($res['ttta']); and stores into array –  Rafee Jun 20 '12 at 7:58
@AlvinWong explode is not working and i even tried with \r\n.. –  Rafee Jun 20 '12 at 8:04
@Rafee Sorry I mistakenly swapped the two arguments, should be $lines_array=explode("\n", $res['ttta']); –  Alvin Wong Jun 20 '12 at 8:10
working good... even i forgot the explode() thanks.. –  Rafee Jun 20 '12 at 8:13
add comment

Your Answer


