Forum Moderators: coopster
I have a form which submits data to the database and this data is then shown on a page. Now when i press the enter key in the form textarea it goes onto a new line, but the new line doesnt appear in the output, how can i make the new line appear in the output?
It works if i type <br> or <p> but i dont want that, so can someone please help me?
I would appreciate it.
Thanks
You are looking for the nl2br [us3.php.net] function. It converts new-line characters to line-breaks: <br/>
Good luck :)
addform.php
---------------------------------------------------------------
<table width="50%" cellspacing="0" cellpadding="2" border="0" class="tablemain">
<form method="post" action="add.php">
<tr>
<td width="20%">
Date:
</td>
<td width="80%">
<?php echo date("D d M Y, g:i a");?>
</td>
</tr>
<td width="20%">
Title:
</td>
<td width="80%">
<input type="text" name="title" />
</td>
</tr>
<tr>
<td width="20%" valign="top">
Comments:
</td>
<td width="80%" valign="top">
<textarea name="news" cols="50" rows="15" id="news"></textarea>
</td>
</tr>
<tr>
<td width="20%">
</td>
<td width="80%">
<input type="submit" value="Add News" />
</td>
</tr>
</form>
</table>
<?php
include 'dbconnect.php';
$title = $_POST["title"];
$news = $_POST["news"];
$date = date("D d M Y, g:i a");
$sqlquery = "INSERT INTO tnews (Title, Body, Date) VALUES ('$title','$news','$date')";
header("Location: display.php");
//print $sqlquery;
$results = mysql_query($sqlquery);
mysql_close($dbc);
?> Again i would greatly appreciate any advice here if at all possible.
Chris.
<?php
include 'dbconnect.php';
$title = [url=http://us2.php.net/manual/en/function.mysql-real-escape-string.php]mysql_real_escape_string[/url]($_POST["title"]);
$news = mysql_real_escape_string([b]nl2br[/b]($_POST["news"]));
$date = date("D d M Y, g:i a");
$sqlquery = "INSERT INTO tnews (Title, Body, Date) VALUES ('$title','$news','$date')";
header("Location: display.php");
//print $sqlquery;
$results = mysql_query($sqlquery);
mysql_close($dbc);
?>
Also make sure to read up on mysql_real_escape_string!
:)
Thanks again mate its much appreciated.
<?php
include 'dbconnect.php';
$title = $_POST['title'];
$news = $_POST['news'];
$pattern = "/(http:\/\/[\w\.]+)/";
$replace = "<a href='$1'>$1</a>";
$title = preg_replace($pattern, $replace, $title);
$news = preg_replace($pattern, $replace, $news);
$title = mysql_real_escape_string($title);
$news = mysql_real_escape_string(nl2br($new));
$date = date("D d M Y, g:i a");
$sqlquery = "INSERT INTO tnews (Title, Body, Date) VALUES ('$title','$news','$date')";
header("Location: display.php");
//print $sqlquery;
$results = mysql_query($sqlquery);
mysql_close($dbc);
?>
Hope this helps :)
Sorry for that typo ;)