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
Solved

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

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php mysql check email already in database. (second check) 7 66
Dynamic Dropdowns 15 32
jQuery force form POST 7 45
Generate PDF from MySQL using PHP 3 19
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

860 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