Forum Moderators: coopster

Message Too Old, No Replies

nl2br() issues

         

Hinden

8:25 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



Hi all, was wondering if you help me out, I'll try to explain my problem as best I can.

I have a form with a textarea so users can update db entries. After the form is submitted it is sent to a validation script where, among other checks, I run the nl2br() function using the following line of code:

$formVars["newsAbstract"] = nl2br($formVars["newsAbstract"]);

This function works, and when I go back to the textarea in the user form I can see the inserted <br /> tags. So if I enter the following text into the textarea of the form:

line1

line2

After I hit submit, the textarea will show the following:

line1<br />
<br />
line2

Now, if I go straight back to the update form and hit submit after changing nothing the textarea shows the following:

line1<br /><br />
<br /><br />
line2

So the script has added an extra 2 <br /> tags eventhough I changed nothing. So I have 2 questions.

1) How do I stop the <br /> tags showing up in the form?

2) How do I stop inserting these extra <br /> tags into the script upon hitting the submit button?

Many thanks in advance for your help! :o)

dreamcatcher

9:58 pm on Sep 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I prefer str_replace().

$string = str_replace("\r\n", "<br>", $string);

Then when you get your data, use:

<textarea>" . str_replace("<br>", "\r\n", $string) . "</textarea>

hughie

10:41 pm on Sep 26, 2004 (gmt 0)

10+ Year Member



not quite sure i'm 100% seeing your problem (it's late here ;-) )
and i might be stating the obvious so..

i would normally only use nl2br when outputing to HTML, not when saving to a database or similar.

i.e.

- user enters data via textarea

- save data into database/array exactly as it was entered

- if the data is going to be sent back to the textarea then you need do nothing with it as it will be formatted the same as when it was submitted.

- if it's going back to HTML use nl2br on it then.

this helps keep the input clean to much around with it at a later date.

make sense? ;-)

Hughie

eggy ricardo

6:36 am on Sep 27, 2004 (gmt 0)

10+ Year Member



Yeah i do the same as Hughie said, just use a text box and put that data straight into the database. Then if i need to edit it, i can put it straight back into the text box. This works fine because text-boxes aren't formatted, so on a formatted output, such as a HTML page, i use the nl2br.

That probably made even less sense but I hope it helps...

Cheers
Richard

coopster

1:40 pm on Sep 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And Welcome to WebmasterWorld, Hinden!

Hinden

6:19 pm on Sep 27, 2004 (gmt 0)

10+ Year Member



Thanks for the all the help, its much appreciated! I'll let you know how it turns out ;o)

Hinden

9:02 pm on Sep 29, 2004 (gmt 0)

10+ Year Member



Just to let you know your help solved the problem. It was indeed the fact that I was using nl2br() on the data before entering it into the DB that caused all the problems.

Thanks again!