Forums

This topic is locked

nl2br Usage

Posted 28 Jan 2007 16:24:28
1
has voted
28 Jan 2007 16:24:28 Darryl Lennon posted:
Hey there

First post and I hope it's in the right place.

I'm writing a form for a website which includes a textarea in which the user can enter a short profile. I've got no problems with the HTML of the form as such...

but... how do I go about using the 'nl2br' function? The example in the PHP.net manual is obscure to say the least, as I'm 'fairly' new to php coding.

So, does the 'nl2br' function simply get put into the html code for the textarea, or for the form or what?

Any examples of coding would be most welcome!

Thanks in advance

Darryl L.

Replies

Replied 29 Jan 2007 02:37:01
29 Jan 2007 02:37:01 Alan C replied:
From the php function docs . . .

string nl2br ( string string )

Returns string with '<br />' inserted before all newlines.

Hmm, it does look a little odd, I'm not sure that there should be two occurences of string inside the brackets, especially as there is no comma between them, that might be an error.

The first string means that the function returns a string, so you always have to call it by assigning it to a variable, like . .

$a=nl2br($info);

The string in the brackets means you put a string there, it can be a variable that contains a string.

I would be wary of converting any newlines to html breaks because they would then be 'hard' returns and could mess up the formatting you your page.

There are some good books on php but I've found the best reference for the functions is the php.net site, there are usually some good examples at the bottom of the page that describes the function, if you read through them and pick the code apart it will help a lot, but . . . some of the people who write them are real experts so it's not always the easiest code to follow, but stick with it.

Can I also suggest that when you get the string from your user via that textbox that you do some checking of it before you let it anywhere near your database. Some people will put strange characters in there. They could put bold tags around their details for example. There are php functions that will strip out html tags and characters that are not a-z A-Z 0-9 and whitespace.

Replied 01 Feb 2007 20:28:03
01 Feb 2007 20:28:03 Darryl Lennon replied:
Hey <img src=../images/dmxzone/forum/icon_smile_shock.gif border=0 align=middle>

Thanks for the response.

I'm still unsure as to how to actually go about linking the nl2br function to my textbox. I'm guessing that it'll be used on the page that actually displays the profile and not the one collecting the information?

My textbox (or field) is called 'profile' so would my function call be: $a=nl2br($profile); ? Then, would you call the dynamic text from the recordset (rsUsers) as per normal, or am I missing something?

Again, sorry if I'm repeating myself in places, I just can't get the penny to drop with this function!

Thanks again in advance,

Darryl L.
Replied 02 Feb 2007 18:09:49
02 Feb 2007 18:09:49 Alan C replied:
assume your form is being sent back to the server using the POST method,

when the information from the form arrives at your server it will be stored in the $_POST array, which is an associative array, it is also global, that means you can access it from anywhere in your scripts and you do that by names, like . . .

$local_profile=$_POST['profile']; // gets the user input that was in the field called profile

now you can do

$local_profile=nl2br($local_profile); // this changes any newlines to breaks

Now you can store that back into the database table.

DW will be doing something like this, you will have to trawl through the code looking for something like . . .

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1" {
$updateSQL = sprintf("UPDATE properties SET profile =%s, WHERE p_id=%s",

GetSQLValueString($_POST['profile'], "text",

(this is not complete, I have removed a lot of the code, the first bit is testing whether or not the form1 has sent input, then is building the query that will write the fields to the database. Look down for the GetSQLValueString function call that picks up the value of profile, what that function does is prepare the value for insertion by adding quotes or whatever as necessary. You could change that line to read something like . . .

GetSQLValueString(nl2br($_POST['profile']), "text",

that will call nl2br before the string is prepared and written to the table, here's an example of what I do

GetSQLValueString(ucfirst($_POST['profile']), "text",

ucfirst is a function that makes the first character Upper Case, I do that to tidy up names because many people don't bother to type their name with a capital then it looks so much better when they see it later.

Hope that helps

Replied 02 Feb 2007 22:34:57
02 Feb 2007 22:34:57 Darryl Lennon replied:
Sorta...

I think I see what you're saying, however upon going through my source code I see that it's using GET rather than POST?

<pre id=code><font face=courier size=2 id=code> if (isset(nl2br($_GET['profile'])) { </font id=code></pre id=code>

Would it work by putting...

<pre id=code><font face=courier size=2 id=code> if (isset($_GET['shout_id'])) { </font id=code></pre id=code>

Thanks again

Darryl L.
Replied 04 Feb 2007 23:19:02
04 Feb 2007 23:19:02 Alan C replied:
yes, there are two methods of passing parameters, GET and POST, the get method strings them all out behind the url, whereas the POST sticks them into the html header so they are "invisible" so just as you have it using the GET array.

Reply to this topic