Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display textarea format on echo from database

I have created a form

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

And a PHP code

<?php
    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

Rafee
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

like image 233
Rafee Avatar asked Jun 20 '12 07:06

Rafee


1 Answers

Use

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

like image 126
Alvin Wong Avatar answered Nov 08 '22 02:11

Alvin Wong