Page 1 of 1
help with text formatting from textarea
Posted: Sat Sep 03, 2005 10:23 pm
by vchris
Hey Guys!
I got a question about php. I have a textarea where users can send me a feedback about my site. What I want to know is how do I keep the text formatting from the textarea so I can display it with the same format?
Posted: Sat Sep 03, 2005 10:24 pm
by feyd
Posted: Sat Sep 03, 2005 10:28 pm
by vchris
Thanks a lot man it works! That was fast!!!

Posted: Sun Sep 04, 2005 6:40 am
by raghavan20
feyd, I dont see htmlentities work for this script.
I got one doubt for a long time. if I use nl2br on a string and add <br />, will htmlentities turn html parts(<br>s) to html equivalents???
Code: Select all
<html>
<body>
<form name = 'frmText'action = '' method = 'post'>
<textarea rows = '12' cols ='60' name = 'taSomething' ><?php if (isset($_POST['taSomething'])) echo $_POST['taSomething']; ?></textarea>
<input type = 'submit' name = 'subText' value = 'submit' />
</form>
</body>
</html>
<?php
if (isset($_POST['subText'])){
echo "<textarea rows = '10' cols = '60'>".nl2br($_POST["taSomething"])."</textarea>";
echo "<br />".htmlentities($_POST["taSomething"])."<br >";
echo "<textarea rows = '10' cols = '60'>".htmlentities($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".htmlentities(nl2br($_POST["taSomething"]))."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags(nl2br($_POST["taSomething"]), "<br><b>")."</textarea>";
}
?>
Posted: Sun Sep 04, 2005 8:30 am
by feyd
you run nl2br on the output of htmlentities.

Posted: Sun Sep 04, 2005 9:22 am
by raghavan20
try the code at
http://raghavan.100webcustomers.com/test.php
Code: Select all
<html>
<body>
<form name = 'frmText'action = '' method = 'post'>
<textarea rows = '12' cols ='60' name = 'taSomething' style="background-color:#000099; color:#FFFFFF; "><?php if (isset($_POST['taSomething'])) echo $_POST['taSomething']; ?></textarea>
<input type = 'submit' name = 'subText' value = 'submit' />
</form>
</body>
</html>
<?php
if (isset($_POST['subText'])){
echo "<textarea rows = '10' cols = '60'>".nl2br($_POST["taSomething"])."</textarea>";
echo "<br />".htmlentities($_POST["taSomething"])."<br >";
echo "<textarea rows = '10' cols = '60'>".htmlentities($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".htmlentities(nl2br($_POST["taSomething"]))."</textarea>";
echo "<textarea rows = '10' cols = '60'>".nl2br(htmlentities($_POST["taSomething"]))."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags($_POST["taSomething"])."</textarea>";
echo "<textarea rows = '10' cols = '60'>".strip_tags(nl2br($_POST["taSomething"]), "<br><b>")."</textarea>";
}
?>
Posted: Sun Sep 04, 2005 9:29 am
by feyd
is there still an issue or are you just showing the results?
Posted: Sun Sep 04, 2005 9:39 am
by raghavan20
sorry I forgot to say that htmlentities is not working when you see the result...

Posted: Sun Sep 04, 2005 9:44 am
by feyd
your second echo in the submission processing block looks exactly as it should for just running htmlentities().
Posted: Sun Sep 04, 2005 9:50 am
by raghavan20
the example from PHP manual
input
should output
but mine outputs as
ofcourse, i see a difference

Posted: Sun Sep 04, 2005 12:28 pm
by feyd
you're viewing it on a webpage. Look at the source code. It has exact what the manual says will output.

Posted: Mon Sep 05, 2005 5:51 am
by raghavan20
Oops...I did not know that I should see the output in the view source rather at the browser.
normally, to disable scripts and preserve line breaks and formatting , should I use...
(or)
Posted: Mon Sep 05, 2005 9:03 am
by feyd
the former.