Trim Text area (charcter return values)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sss123
Forum Newbie
Posts: 11
Joined: Thu Sep 30, 2010 8:11 pm

Trim Text area (charcter return values)

Post by sss123 »

Hi there,

I am trying to trim a textarea called comments in my enquiries form of character returns. I am told the trim function does this and have applied it to my php however it doesn't seem to be working.

<?php
$con = mysql_connect("*********","*********","*********");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("safe", $con);

$number = preg_replace('/[^0-9]/', '', $_POST['number']);

$realname = mysql_real_escape_string($_POST['realname']);

$email = mysql_real_escape_string($_POST['email']);

$comments = trim($_POST['comments']);
$comments = mysql_real_escape_string($_POST['comments']);

$sql="INSERT INTO Enquiries (Name, Number, Email, Comments, Date)
VALUES
('$realname','$number','$email','$comments', CURDATE())";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "";

mysql_close($con)
?>

Can anyone see why this might not be working?

Thanks in advance for any input. It is much appriciated.

Kind regards

Mike
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Trim Text area (charcter return values)

Post by McInfo »

Did you notice the button named "PHP Code" when you posted your question? Please use it.

What evidence tells you that trim() is not working?

Check your form for whitespace immediately after the opening <textarea> tag and immediately before the closing </textarea> tag.
sss123
Forum Newbie
Posts: 11
Joined: Thu Sep 30, 2010 8:11 pm

Re: Trim Text area (charcter return values)

Post by sss123 »

Hello,

Thank you for your reply. I know it isn't working because I uplaod the values to mysql database and the text is still sepparated by newlines.

As regards the whitespace, I'm not too sure. The code for the textarea is:

<TEXTAREA name=comments rows=10 cols=32></TEXTAREA><BR><BR><BR><INPUT type=submit value=Send name=send><BR><BR>&nbsp;</P></FORM>

Thanks again for your input. I really do appriciate it.

Kind regards

Mike
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Trim Text area (charcter return values)

Post by McInfo »

I wasn't kidding about the button named "PHP Code". It really does exist. You can even use it for HTML.

You might be misunderstanding what trim() does. It removes whitespace characters from only the beginning and end of a string.

If you want to remove certain characters from within a string, use str_replace().

If you don't want newlines in your submitted text, why are you using a textarea instead of a text input?
sss123
Forum Newbie
Posts: 11
Joined: Thu Sep 30, 2010 8:11 pm

Re: Trim Text area (charcter return values)

Post by sss123 »

Sorry about the PHP code button. I will use it from now on! : )

I had played with the str_replace() function however couldnt seem to get it to work either. Could you show me where it should be placed withing my original MySQL code?

I would like to keep the text area as it is for comments and questions. The text field is a bit small.

Thanks again for your time and assistance.

Kind regards

Mike
Post Reply