I insert this into the database from a textarea:
Hello
How
Are
You
It gets saved like so:
Hello\n\nHow \n\nAre \n\nYou
But when I display it in the textarea after putting it through nl2br it still shows up as Hello\n\nHow \n\nAre \n\nYou. How do I make it display the same way that I had inserted it into the database?
This is the jQuery code I am using to get the value:
var getURL = WEBROOT + "index.php/Connection/getConfig/" + rowID;
var editConfig = $("<div>");
editConfig.load(getURL, function(){
var configValue = $(this).html();
console.log(configValue);
var button = $("<button>", {
html: "submit"
}),
textArea = $("<textarea>",{
width: "350px", height: "600px"
}).val(nl2br(configValue));
$(this).text("").append(
textArea,
$("<br/>"),
button
).dialog({
title: "Editing Config for id " + rowID,
modal: true,
width: 405
});
});
(Upgrading to an answer)
It sounds like you stored the character sequence
\nin your database rather than an actual newline as represented by that escape code. In that casenl2bris not going to make any difference (it replaces actual line breaks with HTML<br/>tags, which isn’t even what you want in order to display line breaks in a<textarea>) – you need to usestripcslashesinstead.However, I’d recommend fixing the data in your database so that its contents are not escaped.