Solved

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

Posted on 2008-10-25
4
848 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 500 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now