Jump to content

Textarea Form


shadowayex

Recommended Posts

Ok, I'm building a PM system for a site and it works perfect, except if someone sends a message that has returns in it, it doesn't retain them. The column in the database where the message is saved is a TEXT type with utf8_bin collation (I have no idea what that means, but I was told it's a good collation to use). Is there some kind of function I need to add that will keep the returns.

Link to comment
Share on other sites

You mean like this?

[main message here]
[cut-off quote here]
What's the length of the database field?Also, the actual code would probably help us to help you...EDIT: By the way, collation is just the sort-order.
Link to comment
Share on other sites

$mode=$_GET['mode'];if($mode=="newmsg") { echo "<form action='messages.php?mode=sendmsg' method='post'>"; echo "<div>"; echo "To: <input type='text' name='to' value='' /><br />"; echo "Subject: <input type='text' name='subject' value='' /><br />"; echo "Message:<br />"; echo "<textarea rows='10' cols='50' name='message'></textarea><br />"; echo "<input type='submit' name='submit' value='Send' />"; echo "</div>"; echo "</form>";}

That's the section that makes the form for the new message.

if($mode==sendmsg) {$to=mysql_real_escape_string($_POST['to']);$subject=mysql_real_escape_string($_POST['subject']);$message=mysql_real_escape_string($_POST['message']);$check=mysql_query("SELECT Username FROM players WHERE Username='$to'"); if(mysql_num_rows($check)==1) {  $sql = mysql_query("INSERT INTO messages (Receiver, Sender, Subject, Message) VALUES ('$to', '$user', '$subject', '$message')"); } else {  echo "User does not exist. Try again.";  echo "<form action='messages.php?mode=sendmsg' method='post'>";  echo "<div>";  echo "To: <input type='text' name='to' value='' /><br />";  echo "Subject: <input type='text' name='subject' value='' /><br />";  echo "Message:<br />";  echo "<textarea rows='10' cols='50' name='message'></textarea><br />";  echo "<input type='submit' name='submit' value='Send' />"; }}

That part sends the message. It's just a PM system. Like the messaging system on here. The problem is, if they hit the return button to make a new paragraph, it doesn't save the return. It displays it as one paragraph. There's no length limit of the field, since it's a TEXT type field.

Link to comment
Share on other sites

If you're using plain HTML, try this:

$message = str_replace("\n", "<br>\n", $message);

If it's XHTML, try this:

$message = str_replace("\n", "<br />\n", $message);

EDIT: Wait, that's not platform independent. Sec...EDIT2: Rather, use these...HTML:

$message = preg_replace("/(?:\r\n|\r|\n)/", "<br>" . PHP_EOL, $message);

XHTML:

$message = preg_replace("/(?:\r\n|\r|\n)/", "<br />" . PHP_EOL, $message);

Link to comment
Share on other sites

Tried your solution and got this error:Warning: preg_replace(): Compilation failed: nothing to repeat at offset 0 in /home/www/testtools.freehostia.com/news.php on line 35

Link to comment
Share on other sites

What happened is that the line breaks were stored, and if you looked in the source you would have seen them. However, they were not rendered by the browser because as JSG said HTML [renderers] do not display line break characters.

Link to comment
Share on other sites

What happened is that the line breaks were stored, and if you looked in the source you would have seen them. However, they were not rendered by the browser because as JSG said HTML [renderers] do not display line break characters.
Good, call. I see that now. I'll be sure to look for that type of stuff next time.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...