cancel
Showing results for 
Search instead for 
Did you mean: 

HTML textarea problem

Peterworks
Rising Star
Posts: 79
Thanks: 8
Fixes: 2
Registered: ‎17-02-2008

HTML textarea problem

Hi all,
I have created a feedback form in HTML with the following code (I have removed the br after each line):
<form method="post" action="sendmail.php">
  <p align="center">
  Please enter your name: <input name="name" type="text" />
  Please enter your email address: <input name="email" type="text" />
  Please enter your phone number: <input name="phone" type="text" />
  Enter your message:
  <textarea name="comments" rows="15" cols="50"></textarea>
  <input type="submit" value="Send" />
</form>
The sendmail.php does the following:
//Build message string
$message .= "Name: $name\n";
$message .= "Email: $email\n";
$message .= "Phone: $phone\n\n";
$message .= "*** Start of message ***\n";
$message .= "$comments\n";
$message .= "*** End of message ***\n";
$message .= "IP Address: $ipadd\n";
$message .= "* * * * * * * *  * * *\n";
//Build and send email (To: address, Subject, Text of message, From: address)
mail($toadd, $subject, $message, "From: $fmadd");
My problem is that, within the comments string, there is an extra line feed after a hard line feed. This is how it is received in my mail box:
Name: Peter
Email: someone@somewhere.com
Phone: 1234
*** Start of message ***
Test
Now is the time for all good men to come to the aid of the party.
Now is the time for all good men to come to the aid of the party.
End test
*** End of message ***
IP Address: 84.93.110.80
* * * * * * * *  * * *
I have searched and searched but cannot find out why. I have tried the "wrap=" variations but still no good.
Any advice would be most welcome.
Thank you
11 REPLIES 11
7up
Community Veteran
Posts: 15,824
Thanks: 1,579
Fixes: 17
Registered: ‎01-08-2007

Re: HTML textarea problem

Quote from: Peterworks
My problem is that, within the comments string, there is an extra line feed after a hard line feed.

Quote from: Peterworks
Hi all,
I have created a feedback form in HTML with the following code (I have removed the br after each line):
The sendmail.php does the following:
//Build message string
$message .= "Name: $name\n";
$message .= "Email: $email\n";
$message .= "Phone: $phone\n\n";
$message .= "*** Start of message ***\n";
$message .= "$comments\n";
$message .= "*** End of message ***\n";
$message .= "IP Address: $ipadd\n";
$message .= "* * * * * * * *  * * *\n";
//Build and send email (To: address, Subject, Text of message, From: address)
mail($toadd, $subject, $message, "From: $fmadd");
This is how it is received in my mail box:
*** Start of message ***
Test
Now is the time for all good men to come to the aid of the party.
Now is the time for all good men to come to the aid of the party.
End test
*** End of message ***
IP Address: 84.93.110.80
* * * * * * * *  * * *

Erm pardon me but I'm not seeing TWO line feeds here. Your PHP clearly shows one \n at the end of the $comments and then below it the *** End of message ***
Thats exactly what you have show in your sample email. The last line of the $comments variable - 'End test' and then the *** End of message *** line. There is nothing between them.
What you might want to do though is look at using there heredoc instead which makes life a lot easier for things like this:
Quote
$message .= <<< END
Name: $name
Email: $email
Phone: $phone
*** Start of message ***
$comments
*** End of message ***
IP Address: $ipadd
* * * * * * * *  * * *
END;

That works exactly like what you were doing except that what you see is what you get and your variables are still replaced with their values and you don't need to bother concat'ing the $message variable on every line. Also don't forget in your text box, if someone hits the enter key that will be sent to your script unless it is backspaced / deleted. You can use the trim() function to get rid of it.
You'll also want to look into using phpmailer class for outbound emails as it lets you do things a lot easier (eg attachments, setting the from address, specifying CC, BCC etc). The mail function does the job of sending basic email out but it will always carry the servers default email address. phpmailer sets up the appropriate email structure that will then be recognised properly at the other end.
I need a new signature... i'm bored of the old one!
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: HTML textarea problem

Don't know if this is what you mean, but
$comments = str_replace("\r\n", "\n", $comments);

may be needed.
Gabe
Peterworks
Rising Star
Posts: 79
Thanks: 8
Fixes: 2
Registered: ‎17-02-2008

Re: HTML textarea problem

Thanks for your replies 7up and Gabe. Busy day today so will work on it tomorrow and let you know.
Many thanks again
Peter
Peterworks
Rising Star
Posts: 79
Thanks: 8
Fixes: 2
Registered: ‎17-02-2008

Re: HTML textarea problem

Have now progressed and solved my problem.
Thanks Gabe for pointing me in the right direction. Using an ASCII output to the $comments shows that the textarea uses cr then nl (which accounts for the double line feeds).
Have used
$comments = str_replace("\n", "", $comments);
and it works as I wanted.
7up thanks for the heredoc info. Am now using it as the code seems quicker and also it is easier to change any output within the string.
Thanks also for the PHPMailer info. Don't really need it right now but you never know in the future.
Thank you again
Peter
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: HTML textarea problem

If you filter the \n line feeds, it might give you problems with forms returned by Linux or Mac (OS X), which use just a \n for a new line. Windows uses \r\n, but the mail server automatically converts all \n to \r\n, so \r\n becomes \r\r\n, hence two lines. Filtering the carriage return of a \r\n should fix it.
Gabe
7up
Community Veteran
Posts: 15,824
Thanks: 1,579
Fixes: 17
Registered: ‎01-08-2007

Re: HTML textarea problem

You should not be using Gabes suggestion because it will replace ALL instances of \n in your users input in the text box. If they type several paragraphs, you've just deleted the spacing between them  Roll_eyes
As I say, trim() would have done the job perfectly at the beginning and end of the $comments variable:
http://uk3.php.net/trim
Using that would mean you strip the final \n or \r\n from the end without affecting the rest of the users input.
No need to thank me, i clearly have no idea what i'm talking about.. Gabe clearly does even if it does manipulate the users input beyond recognition! (Sorry Gabe! - That's the reason why I did not recommend your fix myself!)
One final thing, phpmailer advice wasn't really optional. You WILL need it so don't put it on the backburner. When you get your outgoing emakils working you're going to realise why I was telling you this. Not only will you have to manually create your own headers but email structure (MIME compliant with yet more headers) and adding attachments will be another mission. PHPmailer does everything for you. Don't delay, download today.
I need a new signature... i'm bored of the old one!
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: HTML textarea problem

Quote from: 7up
You should not be using Gabes suggestion because it will replace ALL instances of \n in your users input in the text box.

No it won't. It will remove \r where it encounters \r\n. The \r will then get put back again automatically, but avoiding \r\r\n.
Quote
trim() would have done the job perfectly at the beginning and end of the $comments variable

but would still have left the double return for every intervening new line.
Gabe
Peterworks
Rising Star
Posts: 79
Thanks: 8
Fixes: 2
Registered: ‎17-02-2008

Re: HTML textarea problem

Thanks again for the input.
I do trim the string before using the replace. This gets rid of any superfluous new lines at the beginning and end of the string but not in the middle.
I have tested the form using a friends Mac (OS X) and it works fine. Don't know anyone with Linux so unable to try it.
Regards,
Peter
P.S. Will download PHPMailer today...
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: HTML textarea problem

It's a mixed bag on Mac at the mo, depending on browser and conditions.
Gabe
7up
Community Veteran
Posts: 15,824
Thanks: 1,579
Fixes: 17
Registered: ‎01-08-2007

Re: HTML textarea problem

Quote from: Peterworks
My problem is that, within the comments string, there is an extra line feed after a hard line feed.

Hang about, have i misunderstood something here?
I understood that as at the end of the $comments variable you had an extra line feed. Now you've just replied it sounds more like you mean inside the message itself - between each line ?
If thats the case then yes I'll go with Gabe actually but my initial understanding of your problem was that you had an unwanted 'return' at the end of the message. Apologies to Gabe if I have misunderstood.
Good move with phpmailer, you'll need it sooner than you realise. By default php sends emails from the server admin address. You have to create your own headers to show your 'from' address. It gets worse too if you want to use html emails or add attachments. phpmailer allows you to do everything easily.
I need a new signature... i'm bored of the old one!
Peterworks
Rising Star
Posts: 79
Thanks: 8
Fixes: 2
Registered: ‎17-02-2008

Re: HTML textarea problem

My HP laptop has HP QuickWeb (which I don't use) but it is apparently using Linux. That being the case I tried it from there and all okay.
I think I will leave it as is as it is working how I want and be prepared for any anomalies that might occur. I can then, if necessary, revisit it.
Thanks again
Regards,
Peter