I built a very simple chat system… it works great and when the user enters a message it displays properly, the only problem is that line breaks don’t show up…. somewhere along the line of sending the info to the database and retrieving it I lose the line breaks…
I am thinking I need to convert \n to <br> somewhere along the way but I really don’t know much about this type of coding… I’ve mostly done games and stuff… No websites or database coding until today (my first day)
Here is my code found on chat.php
<html><head></head><body>
<form action="chat.php" method="post">
Message: <br><textarea type="text" name="message" style="width:80%; height:300px;"></textarea><br>
<input type="submit" />
</form>
<div style="width:100%;">
<?php
$host="***";
$user="***";
$password="***";
$cxn = mysql_pconnect ($host, $user, $password);
mysql_select_db("defaultdb", $cxn);
if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR);
}
$message = strip_tags($_POST["message"]);
mysql_query("INSERT INTO ChatTest (ID, TimeStamp, Message) VALUES ('$ipaddress', NOW(), '$message')");
$data = mysql_query("SELECT * FROM ChatTest ORDER BY TimeStamp DESC") or die(mysql_error());
Print "<table border cellpadding=3 width='100%' style='table-layout:fixed'>
";
Print "<tr>";
Print "<th style='width:10%;'>ID:</th><th style='width:10%;'>TimeStamp:</th><th style='width:70%;'>Message:</th>";
while($info = mysql_fetch_array( $data )) {
Print "
<tr>";
Print " <td>".$info['ID'] . "</td> ";
Print " <td>".$info['TimeStamp'] . " </td>";
Print " <td style='white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word'>".$info['Message'] . "</td></tr>
";
}
Print "</table>";
mysql_close($cxn);
?>
</div></body></html>
Use
nl2br()to convert plain linebreaks to html linebreaks.