Next line down with a <textarea> form.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Next line down with a <textarea> form.

Post 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) ???
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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

Code: Select all

<br />
, but i only want

Code: Select all

<br />
to actually work as HTML.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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:

Code: Select all

echo nl2br($row['tarea']);
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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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>
Post Reply