HTML textarea problem
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Help with my Plusnet services
- :
- Everything else
- :
- HTML textarea problem
HTML textarea problem
09-01-2014 6:01 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
Re: HTML textarea problem
09-01-2014 6:43 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: HTML textarea problem
09-01-2014 9:04 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
$comments = str_replace("\r\n", "\n", $comments);
may be needed.
Gabe
Re: HTML textarea problem
10-01-2014 9:37 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Many thanks again
Peter
Re: HTML textarea problem
11-01-2014 3:35 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
Re: HTML textarea problem
11-01-2014 4:36 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Gabe
Re: HTML textarea problem
11-01-2014 7:42 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator

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.
Re: HTML textarea problem
11-01-2014 8:51 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
Re: HTML textarea problem
12-01-2014 11:39 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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...
Re: HTML textarea problem
12-01-2014 1:24 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Gabe
Re: HTML textarea problem
12-01-2014 1:29 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: HTML textarea problem
13-01-2014 4:41 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page