Jump to content

input text formatting


yoshida
 Share

Recommended Posts

I've written this guest book, but it came to my attention that when you hit enter in the textarea, the line break isn't saved in the database. So everything you type is displayed as one line of text.How can I make sure the line break is displayed without users needing to type <br>?It's a regular text area, saved as a blob.This problem is semi-crucial to finishing my index.php

Link to comment
Share on other sites

You could try using the string replace function:

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

It should replace all the carriage returns with <br>. If that doesn't work, try replacing n with \n.

Link to comment
Share on other sites

I'm stupid, where should I implement this?

if ($pageid == 'gastenboek') {	$command=$_GET['command'];	if ($command == 'write') {		//write section;			if (isset($_POST['akkoord'])) {			//handler;					$gnam=$_POST['gnam'];			$gmal=$_POST['gmal'];			$datum=$_POST['datum'];			$gdat=$_POST['gdat'];			$page=$_POST['page'];			$gcon=$_POST['gcon'];						if ($gnam == "" || $gcon == "") {				//checks if a user actually left his name and message after the beep;				echo "<div id=warning>Niet alle verplichte velden zijn ingevuld. <meta http-equiv=refresh content=\"3; url=index.php?pageid=gastenboek&command=write\">";			}									else {				$query="INSERT INTO gastenboek VALUES ('','$gnam','$gmal','$page','$datum','$gdat','$gcon')";				mysql_query($query);				echo "<div id=warning>Uw bericht is toegevoegd, dank u wel.<meta http-equiv=refresh content=\"3; url=index.php?pageid=gastenboek\"></div>";			}		}				else {			//form;			echo "<form action=index.php?pageid=gastenboek&command=write method=post>";			$date=date('ymdHi');			$maand_array = array("januari", "februari", "maart", "april","mei", "juni", "juli", "augustus", "september","oktober", "november", "december");			$datum = date("j ") . $maand_array[date("n") - 1] . date(" Y");			echo "<table><tr><td><b>Naam:</b><td><input type=text name=gnam size=40></tr>";			echo "<tr><td><b>E-mailadres:</b><td><input type=text name=gmal size=40></tr>";			echo "<tr><td><b>Website:</b> http://<td><input type=text name=page size=40></tr></table><br>";			echo "<b>Datum:</b>$datum<input type=hidden name=gdat value='$date'><input type=hidden name=datum value='$datum'>";			echo "<br><textarea rows=20 cols=88 name=gcon></textarea><br><input type=submit name=akkoord value='voeg toe'>";		}	}		else {		//loads the guestbook from the database, and sorts everything the same way as the newspage does;		//This database however, stores everything as yymmddhhmm to sort everything by minute;		//The date dd month yyyy is stored separately;		$query="SELECT * from GASTENBOEK ORDER BY 'gdat' DESC";		$result=mysql_query($query);				$num=mysql_num_rows($result);		echo "<div id=schaduw><div id=header>Gastenboek</div></div>";		echo "<div id=schaduw><div id=werklaag>";		echo "<a href=index.php?pageid=gastenboek&command=write>Voeg een bericht toe</a>";		echo  "<p><table cellspacing=0 cellpadding=0>";				$i=0;		//The userdetail table and the message table are loaded next to each other, in the main table;		//The table echo is been split by row, to enhance read- and editability. Never hurts to have more rows...  ;		while($i < $num) {			$name=mysql_result($result,$i,"gnam");			$mail=mysql_result($result,$i,"gmal");			$datum=mysql_result($result,$i,"datum");			$gcon=mysql_result($result,$i,"gcon");			$page=mysql_result($result,$i,"page");			echo "<tr><td valign=top>";			//userinfo table;			echo "<table cellpadding=0 cellspacing=0 border=1 width=300>";			echo "<tr><td height=30 width=110><td></tr>";			echo "<tr><td bgcolor=#606060><td bgcolor=#606060></tr>";			echo "<tr><td><b>Naam</b><td>$name</tr>";			echo "<tr><td><b>E-mail</b><td><a href=mailto:$mail>$mail</a></tr>";			echo "<tr><td><b>Homepage</b><td><a href=http://$page target=0>$page</a></tr>";			echo "<tr><td><b>Geplaatst op</b><td>$datum</tr></table>";			//spacer;			echo "<td width=1 bgcolor=#606060>";			//message table;			echo "<td valign=top><table cellspacing=0 cellpadding=0 border=1>";			echo "<tr><td height=30></tr>";			echo "<tr><td bgcolor=#606060></tr>";			echo "<tr><td>$gcon</tr></table></tr>";			$i++;		}		echo "</table></p>";		echo "</div></div>";	}}

Link to comment
Share on other sites

What is this, German - Dutch :) This is the line that pulls out the info that was entered into the textarea, right?

$gcon=mysql_result($result,$i,"gcon");

So add this line

$gcon=mysql_result($result,$i,"gcon");$gcon=str_replace("n","<br>",$gcon);

Link to comment
Share on other sites

What is this, German - Dutch  :)
Dutch :)
This is the line that pulls out the info that was entered into the textarea, right?
$gcon=mysql_result($result,$i,"gcon");

That's the line that reads the info from the database, $gcon is echoed into a table a few lines later.$gcon=$_POST['gcon']; (in the handler - read comment) pulls it out, and into the query. I believe I should put $gcon=str_replace("n","<br>",$gcon); there?I'll play around with it for a while :) Had to use str_replace for smileys anyway, still learning how to get that right.Thanks.
Link to comment
Share on other sites

That's the line that reads the info from the database, $gcon is echoed into a table a few lines later.$gcon=$_POST['gcon']; (in the handler - read comment) pulls it out, and into the query. I believe I should put $gcon=str_replace("n","<br>",$gcon); there?
Yes, basically put the replace string after the info is pulled from the database but before it's printed to screen :)
  1. read textarea from database
  2. replace \n with <br>
  3. print to screen

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...