Solved

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

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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 …

738 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