Page 1 of 1
Next line down with a <textarea> form.
Posted: Fri Apr 01, 2005 9:39 am
by Dale
How do i get it so when someone types something on the next line down, it saves to the database knowing that, so when I read it from the database it shows with the next line down. (If you understand what i mean) ???
Posted: Fri Apr 01, 2005 9:42 am
by Burrito
if you mean you're outputting to html and you want to see the carriage returns: try nl2br() on your textarea string before you insert to your db.
Posted: Fri Apr 01, 2005 9:52 am
by Dale
Burrito wrote:if you mean you're outputting to html and you want to see the carriage returns: try nl2br() on your textarea string before you insert to your db.
Right, i've used that now, but im also using
htmlspecialchars() to stop </td></tr></table> being posted. So it shows
, but i only want
to actually work as HTML.
Posted: Fri Apr 01, 2005 10:01 am
by Burrito
not sure I understand. Why would you have </td> etc in your textarea?
the original problem was to have textareas with carriage returns display those on the html page no?
if that's the case, then do just what I said (either before your insert to the db or right when you display it on the page)
ex:
Code: Select all
<?if(isset($_POST['mytextarea'])){
$newarea = nl2br($_POST['mytextarea']);
mysql_query("insert into mytable (tarea) values ('".$newarea."')")
or die(mysql_error());
}
?>
<body>
<form method="post">
<textarea name="mytextarea"></textarea>
</form>
</body>
alternatively, you could use nl2br when you're outputting the string on your html page.
ie:
that should only replace the \r\n stuff with <br>...which is all you should have in your textarea unless you're doing some kind of HTML coding in there
Posted: Fri Apr 01, 2005 10:09 am
by Dale
I'm doing like a guestbook script, but if </td></tr></table> was entered by someone, it would close the table and mess up the page.
Posted: Fri Apr 01, 2005 10:22 am
by Burrito
ok, I understand the problem now. I'm not 100% sure on this, but I think if you run the htmlentities or specialchars function before you do the nl2br (as you're inserting to the db) it won't do anything with \r\n. Then you could run nl2br on the output and it would catch the \r\n's and not muck up your other html chars that you want changed (to not screw up your table formatting).
alternatively, you could use mutilple str_replaces or a preg_replace to get rid of the </td>s etc before you insert to the db.
I'd try the former first and if that doesn't work, then look into replacing the "bad" stuff.
Posted: Fri Apr 01, 2005 4:59 pm
by nickvd
Insert the text into the db as you would normally (be sure to protect against injections though). On it's way to the user's page, just pass it through htmlentities then nl2br..
Code: Select all
<?php
if(isset($_POST['mytextarea'])){
$newarea = $_POST['mytextarea']; // protect it against sql injections by your favourite method
mysql_query("insert into mytable (tarea) values ('".$newarea."')")
or die(mysql_error());
}
$result = mysql_query("SELECT * from `mytable`");
$row = mysql_fetch_row($result);
echo nl2br(htmlentities($row['tarea']));
/*
This should do it, but i never tested it, i rarely interact with mysql directly, i use a db abstraction class
*/
?>
<body>
<form method="post">
<textarea name="mytextarea"></textarea>
</form>
</body>