Forum Moderators: coopster

Message Too Old, No Replies

Textbox that adds <br> after each return

         

PokeTech

9:39 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



I'm in need of a text box that when you enter something it adds <br> after each line entered so like this:

hey
whats up?
bye

result

hey<br>
whats up?<br>
bye<br>

That what I want it to look like when its posted in the mysql. Anything thoughts on how to do that? Please help!

topr8

9:59 pm on Apr 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



when you get the form data just replace a new line with a "<br>"

with windows it is something like &vbCrlf

for linux i think it is /n

(these are the representations of a new line - it's very late here sorry if my memory fails me)

FourDegreez

11:06 pm on Apr 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You want the nl2br function.

PokeTech

11:44 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



I'm sorry but where exaclty would I put this "nl2br" or "/n" thing? Well I know what they are but how do I get it to work in a form?

PHP_Chimp

9:18 am on Apr 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A basic example

<form method="post" action="some.page">
<textarea id="my_text">
Some text
in your text
area.
</textarea>
<input type="submit" value="send">
</form>

Now the php

// this on some.page
if ($_POST['submit']) { // form submitted
$raw_text = $_POST['my_text'];
$html_text = nl2br($_POST['my_text']);
echo "<p>Raw text:<br />$raw_text</p>";
echo "<p>Formatted Text:<br />$html_text</p>";
}
else {
// form not submitted
}

Run that and have a look at the result.

bkeep

9:18 am on Apr 8, 2008 (gmt 0)

10+ Year Member



$text = "foo is not\n bar";
print nl2br("$text");

PokeTech

1:05 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



PHP_Chimp that didn't work for some reason, either that or I'm doing something wrong.

PHP_Chimp

3:27 pm on Apr 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok. Try this full version.

<?php
$form = <<<FORM
<form method="post" action="{$_SERVER['SCRIPT_NAME']}">
<textarea id="my_text" name="my_text">
Some text
in your text
area.
</textarea>
<input type="submit" value="send" name="Submit" />
</form>
FORM;
if (array_key_exists('Submit', $_POST)) {
$raw_text = $_POST['my_text'];
$html_text = nl2br($_POST['my_text']);
echo "<p>Raw text:<br />$raw_text</p>";
echo "<p>Formatted Text:<br />$html_text</p>";
}
else {
echo $form;
}
?>

Not much style, but works ;)

PokeTech

3:47 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



Sweet thanks, it works!

PokeTech

8:47 pm on Apr 8, 2008 (gmt 0)

10+ Year Member



I have another problem... How would I get that so it works when storing data in a mysql?

supermanjnk

1:53 am on Apr 9, 2008 (gmt 0)

10+ Year Member



Honestly, it would be better to store the information as it is when storing it in sql, and just make the change when the information is viewed.

All you need to do is use the nl2br() function on the retrieved information when you go to display it, for instance.
<?
$q = "SELECT my_text FROM table WHERE id='1';
$results = mysql_query($q);
$data = mysql_fetch_object($results);
echo nl2br($data->my_text);
?>

The issue you will run into when making the change before inserting it into the database is that if you allow updating later on, it will continue to add breaks every time thus ending up with more line breaks then you want.

However, since you asked:

An example would be something like this"
<?
$q = "INSERT INTO table (text) VALUES('".nl2br($_POST["my_text"])."') ";
?>
or
<?
$q = "UPDATE table SET text='".$_POST["my_text"]."'";
?>

PHP_Chimp

9:29 am on Apr 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



While I agree with the above I would just like to change the last example:

<?php
$q = "UPDATE table SET text='".[b]mysqli_real_escape_string[/b]($_POST["my_text"])."'";
?>

As you should NEVER use unchecked user data.

Just seeing any sql with no escaping gives me nightmares ;)

PokeTech

12:21 pm on Apr 9, 2008 (gmt 0)

10+ Year Member



Thanks a bunch that really cleared it up!