Next line down with a <textarea> form.
Moderator: General Moderators
Next line down with a <textarea> form.
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) ???
Right, i've used that now, but im also using htmlspecialchars() to stop </td></tr></table> being posted. So it showsBurrito 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.
Code: Select all
<br />Code: Select all
<br />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:
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
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>ie:
Code: Select all
echo nl2br($row['tarea']);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.
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.
-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
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>