Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-10-25
4
Medium Priority
?
856 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

963 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