Forum Moderators: coopster

Message Too Old, No Replies

Textarea new lines

how can i make new lines in a textarea

         

bysonary

11:47 pm on Nov 15, 2006 (gmt 0)

10+ Year Member



hello,

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

eelixduppy

11:49 pm on Nov 15, 2006 (gmt 0)



Welcome to Webmasterworld!

You are looking for the nl2br [us3.php.net] function. It converts new-line characters to line-breaks: <br/>

Good luck :)

bysonary

12:01 am on Nov 16, 2006 (gmt 0)

10+ Year Member



does this go in my form? or in my processing script? I have included my full form below including its formatting hope this is ok, i dont have a clue how to make this nl2br work by looking at it some advice would be great, my code is below.

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>

---------------------------------------------------------------
add.php
---------------------------------------------------------------
<?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.

eelixduppy

12:04 am on Nov 16, 2006 (gmt 0)



It would go in your processing file:

<?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!

:)

bysonary

12:20 am on Nov 16, 2006 (gmt 0)

10+ Year Member



Thanks mate that really helped :-) while we are here i dont suppose you could tell me if it is possible for the same textarea to automatically create a link when [address.com...] is entered, at the minute it just appears as text and well I need to enter the html for it to work and since this is for someone who doesnt know html then It would be prudent to include a work around, do you know of any and if so could you please enlighten me as you already have done with the nl2br.

Thanks again mate its much appreciated.

eelixduppy

2:42 am on Nov 16, 2006 (gmt 0)



This is done with regular expressions:

<?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 :)

bysonary

11:15 am on Nov 16, 2006 (gmt 0)

10+ Year Member



unfortunately i tried that and nothing was saved into the database, not sure where its going wrong, ill look into it and try to get it working, but if anyone else knows the problem then it would be appreciated if you could point it out

bysonary

11:18 am on Nov 16, 2006 (gmt 0)

10+ Year Member



$news = mysql_real_escape_string(nl2br($new));

this was the problem haha i just copied and pasted it and the $new should have been $news, sorry being a bit impatient there.

Thanks mate its working now :) thats great

eelixduppy

11:56 am on Nov 16, 2006 (gmt 0)



I'm glad you got everything sorted out!

Sorry for that typo ;)