Jump to content

php loosing empty line on POST transfer?


domk

Recommended Posts

hi there

 

i just hit a problem with a very simple background:

 

<?
echo '<form method="post" action="?">
<textarea name="content" cols="50" rows="20" id="content">'.$_POST["content"].'</textarea>
<input type="submit">
</form>';
?>

 

so what i'm doing here is calling the sent data via $_POST and by doing so i realized that if you enter an empty line on the beginning of your textarea text it will be vanished on the next call....

fun to watch when you enter five empty lines and by switching the submit query watch the text move up but really not meant to do that...

 

i guess im overlooking something really simple here but i just cant get around it so any feedback is appreciated.

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/124226-php-loosing-empty-line-on-post-transfer/
Share on other sites

what am i missing here?

 

your suggestion will add an empty line to the end instead of taking one from the top  :)

i guess i just have to replace the empty line with hidden placeholders like \n\r and convert them on display....

 

very strang though as it seems it is a bad bug

 

any other ideas?

You would need to do it like this (tested) to avoid the problems -

<?php
echo '<form method="post" action="?">
<textarea name="content" cols="50" rows="20" id="content">
'.$_POST["content"].'</textarea>
<input type="submit">
</form>';
?>

 

The w3.org specification does not state that a newline is required after the closing > of the <textarea tag, but both IE and FF take the first newline from any data you put there if the markup does not have a newline.

When you redisplay text entered in a textarea you need to use the nl2br() function to change the newline characters to <br /> and a newline character.

<?php
echo '<form method="post" action="">
<textarea name="content" cols="50" rows="20" id="content">'.nl2br($_POST["content"]).'</textarea>
<input type="submit">
</form>';
?>

 

Ken

When you redisplay text entered in a textarea you need to use the nl2br() function to change the newline characters to <br /> and a newline character.

<?php
echo '<form method="post" action="">
<textarea name="content" cols="50" rows="20" id="content">'.nl2br($_POST["content"]).'</textarea>
<input type="submit">
</form>';
?>

 

Ken

 

I'm not so sure about that one.

ok thanks for the answers :)

 

<?php
echo '<form method="post" action="?">
<textarea name="content" cols="50" rows="20" id="content">
'.$_POST["content"].'</textarea>
<input type="submit">
</form>';
?>

 

that did the trick. thanks to PFMaBiSmAd for that one.

 

<b>though</b> if you do not include a new line into your input it'll show a space(and add each time you submit) before your text much like:

| text|

 

instead of

|text|

 

but i can live with that as html does not display more then one space...

 

i did actually play around with that a little but did not seem to find any reaseneble conclusion...

very strange though as i think it is most definetely a bug...

 

btw nl2br would print out a br tag for each new line which is visible in textareas so that'll defeat the purpose.

 

good work!... and for me "back to work" ;)

...

though that error is caused be my code sorting where i add a space up front to see clauses much like:

 

if(something) {
echo '<form method="post" action="?section=content&action='.trim($_GET['action']).'&do=save" onsubmit="return(check(this));">
<textarea name="content" cols="50" rows="20" id="content">
'.$mCont["content"].'</textarea>
<input type="submit">
</form>";
}

 

so i have to make it

 

if(something) {
echo '<form method="post" action="?section=content&action='.trim($_GET['action']).'&do=save" onsubmit="return(check(this));">
<textarea name="content" cols="50" rows="20" id="content">
'.$mCont["content"].'</textarea>
<input type="submit">
</form>";
}

 

and now it works like a charm

 

p.s.: mark as SOLVED

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.