Hey,

I have a textfield and before I insert it into the database I run the function nl2br();

It works grand add's <br>'s at brake lines...

However when I output it back into a textfield to edit it and update the record it had the <br>'s? Not a brake line...

Help!

Dan

Recommended Answers

All 7 Replies

Member Avatar for diafol

Well of course it will. The textarea will show the whole string, including any html you've put into it.

Keep the DB version clean (newlines only).

echo nl2br version to the page, for basic html output.
echo clean version to the textarea for editing.

Well of course it will. The textarea will show the whole string, including any html you've put into it.

Keep the DB version clean (newlines only).

echo nl2br version to the page, for basic html output.
echo clean version to the textarea for editing.

Will it keep the brake lines though? Even if you keep it clean in the database?

Dan

Member Avatar for diafol

What do you mean keep it?
You're only using this nl2br() to display it on a page right? You're not storing it in any way, are you?

What I do is use nl2br() to save the line brakes then inset it into the database...

When when the user edits it it has <br>'s not brake lines... do you get me? Or do I need to call nl2br(); to replace <br> with line brakes?

Dan

Member Avatar for diafol

This is the whole point of keeping clean version in DB, to avoid this mess. Don't save the <br /> at all. Only use nl2br() when you want to display the data in html (which is temporary of course).

<div>
<?php echo nl2br($datafromDB);?>
</div>

and in edit mode:

<textarea><?php echo $datafromDB;?></textarea>

When the data is edited and resubmitted, you just use mysql_real_escape_string() again to store it (no <br /> anywhere near it).

Bingo! Thanks =)

Member Avatar for diafol

OK, if we're solved, click the link below the edit box.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.