8

I have an input textarea and I would like to store the line breaks that a user makes in the text area in the database, so that it echoes the output text the same way as in the input text

eg:

Some text some text some text some text


new line some text some text new line 

new line some text new line some text

this is my current upload script:

$sql = "insert into people (price, contact, category, username, fname, lname, expire, filename) values (:price, :contact, :category, :user_id, :fname, :lname, now() + INTERVAL 1 MONTH, :imagename)";
$q = $conn->prepare($sql) or die("failed!");
$q->bindParam(':price', $price, PDO::PARAM_STR);
$q->bindParam(':contact', $contact, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->bindParam(':user_id', $user_id, PDO::PARAM_STR);
$q->bindParam(':fname', $fname, PDO::PARAM_STR);
$q->bindParam(':lname', $lname, PDO::PARAM_STR);
$q->bindParam(':imagename', $imagename, PDO::PARAM_STR);
$q->execute();
2
  • As per Micah's comment, try adding nl2br to your output, i.e. nl2br($imagename);. Commented Oct 4, 2012 at 14:35
  • thanks for your reply, say if it was the lname output that i wanted to preserve the line breaks, how i would do that and where i would place Commented Oct 4, 2012 at 14:39

4 Answers 4

34

The line breaks in the text area are linebreak characters such as \n. These are not rendered in HTML and thus they won't show up if you simply echo the output. You can echo inside of a <textarea> or a <pre> tag or you can use the nl2br() to replace new lines with <br> tags so that it can be displayed as HTML.

Sign up to request clarification or add additional context in comments.

7 Comments

thankyou for your reply, i am unsure where i would put the nl2br(), it is the "lname" text that i want to preserve the line breaks
That would be in the PHP that displays the output. That is, the linebreaks are stored in the DB but the display of that data needs to be parsed before displaying as HTML. echo nl2br($data);
thanks, i have tried that but it literally say nl2br(some text); when i echo
echo "<div id='expanderContent' style='display:none'><div class='reasonbar'><div class='prod-title1'>". nl2br($lname); ."</div><div class='reason1' style='width: 29%;'>Contact:<br /> ". $row['contact'] . "</div></div>";
Sorry, remove the semicolon after nl2br($lname)
|
2

You can escape the line breaks before storing by using mysql_real_escape_string

Documentation here: http://php.net/manual/en/function.mysql-real-escape-string.php

Comments

0

you can also use javascript to break the text as \n to <br> by using replace

str.replace("\n", "<br />","g");

1 Comment

-1 It be best to avoid a JS solution so that users who disable/can't use JS still have the strings render correctly.
0

I had the same problem, and this is working now :

$query = "UPDATE your_table 
         SET your_text_field = 'line 1'" . '\n' . "'line2' 
         WHERE your_id_field = " . $id . "';";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.