Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I enter a textarea into my database and include line breaks?

Posted on 2008-10-25
4
Medium Priority
?
855 Views
Last Modified: 2010-08-05
I've got a tbody of text in a textfield that has several line breaks in it. How do I write an INSERT statement that maintains those line breaks? What do I have to do to "$text" variable to ensure that the integrity of line breaks and paragraphs is inserted correctly?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

by:bui_trung_hieu
ID: 22805757
You can use javascript to replace line-break characters /\n/ in textarea with HTML line-break tag <br /> before submit form like the example below

Hope this helps.

<form action="" name="theForm" method="post">
<div>
	<p><textarea name="txt"></textarea></p>
    <p><input type="hidden" name="hid" value="" />
	<p><input type="button" value="Submit" onClick="javascript: submit_it()"> </p>
</div>
</form>
<script type="text/javascript">
function submit_it(){
	var a;
	a = document.theForm.txt.value.replace(/\n/g, '<br />');
	document.theForm.hid.value = a;
	document.theForm.submit();
}
</script>

Open in new window

0
 
LVL 3

Expert Comment

by:raminhos
ID: 22806166
Before you enter your textarea var into database just do a string replace..

here's an example..

 
str_replace("\n","<br>",$textarea);
 
$sql = "insert into table ('$textarea')";

Open in new window

0
 
LVL 3

Expert Comment

by:raminhos
ID: 22806167
Sorry..
$textarea = str_replace("\n","<br>",$textarea);
 
$sql = "insert into table ('$textarea')";

Open in new window

0
 
LVL 3

Accepted Solution

by:
DarkFish earned 2000 total points
ID: 22806629
When you insert data from a text area to a database the line breaks and paragraphs are usually stored correctly in the database without any additional manipulation. However, if you were to pull that information from the database and then display it on a page, you would notice that you indeed did not have any line breaks or formatting.

The database will store your line breaks, however when rendered by default in a browser, the browser will not insert line breaks as you do not have any break rule - <br /> - tags in your text to insert those breaks.

If you wanted to display that text on the page your best bet is to use a function called nl2br(); essentially it's a default PHP function which converts all new line breaks in your text to a <br /> tag.

Your code would then look something like this:
$text = $_POST['textarea'];
$textBr = nl2br($text);
 
$sql = "INSERT INTO 'table' ('".$textbr."');";

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question