Forum Moderators: coopster

Message Too Old, No Replies

Text Formatting

         

don_dr

7:12 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



I have a form on my website which a user is supposed to use to enter a title and description for job ads into a database using the <textarea> tag for the description field.

The problem i am having is that the information displayed from the database is not properly formatted. Whilst testing it, i entered a couple of paragraphs into the description field, but when it is fetched from the database all the text is joined together in one huge messy cluster.

I need help with formatting the text so that it appears the way it was entered. Dont know if i should have posted this here, but........

thanks in advance

PHP_Chimp

7:19 pm on Aug 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When text is entered into a text area there are no <br /> tags in there. So when you echo this out into html you loose all of the line breaks; they should be there if you look at the source code.
Have a look at nl2br [uk.php.net] as this will convert the new line characters into break tags so you get the formatting that you want.

don_dr

7:43 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



so lets say i have the following

mysql_select_db.....

$example_query = mysql_fetch_assoc($somequery, $string);


Are u suggesting i do this;

echo nl2br($example_query['description'])

don_dr

7:52 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



cos i was thinking the formatting should be done before it is entered into the database. how do i do this?

don_dr

8:25 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



Okay. that settles that. now do you happen to know of a code i can use to display the number of characters the user has typed in the text area?

Thanks.

PHP_Chimp

8:49 pm on Aug 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That would be javascript. As you would need to count the string length on the client side. Unless you just want an answer after they have posted there comments.

don_dr

9:13 pm on Aug 26, 2008 (gmt 0)

10+ Year Member



actually, i was thinking more like trying to limit the number of characters a user is allowed to enter.

i would like to display a number which reduces according to user input. i've seen this done on some other sites.

thanks again.

IndiaMaster

5:05 am on Aug 27, 2008 (gmt 0)

10+ Year Member



In one of my site I used the following:
<textarea name="message" cols="60" rows="10" class="textbox" id="message" onKeyUp="countChar();"></textarea>

The "countChar()" function is as follows:
function countChar()
{
var msg=document.getElementById('message').value;
if(document.getElementById('message').value.length==1025)
{
alert("You have execeeded the charater limit.");
var len=msg.length;
document.getElementById('message').value=msg.substr(0,len-1);
}
document.getElementById('chars').innerHTML=msg.length;
}

Hope it helps.

don_dr

9:01 am on Aug 27, 2008 (gmt 0)

10+ Year Member



Thanks India Master. It's just what i was searching for.

PHP_Chimp

10:30 am on Aug 27, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As javascript can be turned off you may also want to add server side validation.

if ([url=http://uk2.php.net/manual/en/function.strlen.php]strlen[/url]($input) > 1024) {
// echo back the form with a warning
}
else {
// all ok
}

This will hopefully not be used, as the javascript will catch most people. However there is no guarantee that javascript will work, so server side validation may well save you some trouble later down the line.