/    Sign up×
Community /Pin to ProfileBookmark

preserving line breaks in textare

hi, im trying to preserve the line breaks in a textarea, the problem arises when echoing the post back into the text area during the validation section of the script. i know that the line breaks are still there as when i do nl2br, the <br/> tags show in the text area. basicly when the page reloads all the text goes on one line but obviously i want to display the text with the line breaks active.

the code im using is

[code=php]<p> <label for=”body”>Message</label><textarea name=”body” id=”body” rows=”15″ style=”width : 320px;”><?php if(isset($_POST[‘body’])) echo $_POST[‘body’];?></textarea></p>[/code]

this displays the text on one line. if i use nl2br as such

[code=php]<p> <label for=”body”>Message</label><textarea name=”body” id=”body” rows=”15″ style=”width : 320px;”><?php if(isset($_POST[‘body’])) echo nl2br($_POST[‘body’]);?></textarea></p>[/code]

the text still displays on one line but with the <br/> tags in.

please help

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — Use the "wrap" attribute...[code=php]// Will display NL but send the text as one line
<textarea cols='99' rows='99' wrap='virtual'>
// Will display NL and send NL with the text
<textarea cols='99' rows='99' wrap='physical'>
// Will not display NL and will not send NL with the text
<textarea cols='99' rows='99' wrap='off'>[/code]
Copy linkTweet thisAlerts:
@furnissgauthorFeb 14.2007 — thanks for the quick response, unfortunatly setting the wrap doesnt make a difference to how the text is displaying.

tried to set it to 'virtual', 'physical', 'hard' and 'soft' the echo still displays as one line.

if i take the post and display it outside the text area using nl2br it works fine just echoing inside shows it all on one line.

its starting to drive me mad now.
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — It's strange... See: http://www.nightshift58.com/webdev/test_textarea.php

Here's the code:[code=php]<?php
$test_string = "this is a long running line with which I hope to demonstrate the various wrap mode of an HTML textearea.nnhello worldnhello worldnhello world";
?>
<br>Test 1: No attributes<br>
<textarea name="body" id="body" rows="5" style="width:320px;">
<?php echo $test_string ?>
</textarea>
<br>Test 2: virtual<br>
<textarea name="body" id="body" rows="5" style="width:320px;" wrap='virtual'>
<?php echo $test_string ?>
</textarea>
<br>Test 3: physical<br>
<textarea name="body" id="body" rows="5" style="width:320px;" wrap='physical'>
<?php echo $test_string ?>
</textarea>
<br>Test 4: off<br>
<textarea name="body" id="body" rows="5" style="width:320px;" wrap='off'>
<?php echo $test_string ?>
</textarea>
[/code]
Copy linkTweet thisAlerts:
@furnissgauthorFeb 14.2007 — thanks, resolved the problem wrap did work, there was a function running elsewhere in the framework that was preventing the line breaks from displaying.

Thanks for the help, much appreciated
Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — You're welcome!
×

Success!

Help @furnissg spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...