<textarea> output with <br>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vinpkl
Forum Newbie
Posts: 9
Joined: Tue Aug 12, 2008 5:34 am

<textarea> output with <br>

Post by vinpkl »

hi all

i would like to ask that i have a <textarea> and in that i can insert 3 lines. i want to that 3 lines ouput should come with a break after each line. means i just want to add <br> after each line ouput.
the name of <textarea> is shipping_detail.

Code: Select all

 
$shipping_detail=$_REQUEST['shipping_detail'];
 
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: <textarea> output with <br>

Post by Hannes2k »

Hi,
if you hit 'enter' in a textbox, this line break is represented by \n (Unix System) or \r\n (Windows) or \r.
One solution is to replace \r\n, \n and \r by <br>, but the better solution is to use nl2br:
echo nl2br($value_from_textbox);
vinpkl
Forum Newbie
Posts: 9
Joined: Tue Aug 12, 2008 5:34 am

Re: <textarea> output with <br>

Post by vinpkl »

Hannes2k wrote:Hi,
if you hit 'enter' in a textbox, this line break is represented by \n (Unix System) or \r\n (Windows) or \r.
One solution is to replace \r\n, \n and \r by <br>, but the better solution is to use nl2br:
echo nl2br($value_from_textbox);
hi

here is what i tried. but i gets error "error inserting product"

Code: Select all

 
if(isset($_REQUEST['submit']))
{   
    $shipping_detail = nl2br($_REQUEST["shipping_detail"]);
    $warranty=$_REQUEST['warranty'];
 
$insertqry="insert into product_table(shipping_detail,warranty) values('$shipping_detail','$warranty')";
    
if(mysql_query($insertqry))
$msg="shipping Product added successfully";
else
$msg="Error Inserting new product";
}
 
 
but this doesnt work.

If i m wrong then correct me. do i have to add nl2br where i m writing echo $value in my page or where i have inserted it in my above code.
i m little confuse.
Hannes2k
Forum Contributor
Posts: 102
Joined: Fri Oct 24, 2008 12:22 pm

Re: <textarea> output with <br>

Post by Hannes2k »

Hi,
no, I is better to use nl2br only on the output. Just insert the orginal value into your database and when you use echo to present this data, use nl2br().

But that's not the problem of your mysql-error.

Better try this:
if(mysql_query($insertqry))
echo "shipping Product added successfully";
else
echo die("Error Inserting new product ".mysql_error());


The mysql_error() shows you the error in your statement, so you can fix it.
vinpkl
Forum Newbie
Posts: 9
Joined: Tue Aug 12, 2008 5:34 am

Re: <textarea> output with <br>

Post by vinpkl »

Hannes2k wrote:Hi,
no, I is better to use nl2br only on the output. Just insert the orginal value into your database and when you use echo to present this data, use nl2br().

But that's not the problem of your mysql-error.

Better try this:
if(mysql_query($insertqry))
echo "shipping Product added successfully";
else
echo die("Error Inserting new product ".mysql_error());


The mysql_error() shows you the error in your statement, so you can fix it.
hi

thanks for the solution. its now fine.
i used nl2br where i have written echo $value and it worked perfect.

vineet
Post Reply