Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

PHP Contact Form - Text Area Doesn't Send!?

Hi,

I watched the Contact Form tutorial and have spent time emulating it to fit my needs for a side project. I didn't know any PHP at all before this but I have managed to get the form to send the message - except one problem:

The text area doesn't send along with the rest of the message. I believe I have linked the textarea to the form and set the php variables correctly.

Can someone please tell me what I am doing wrong. Also, I am not doing the line breaks correctly in the sent message so double thanks if you can help me with that as well!

Here's the relevant excerpts:

<form action="index.php" method="post" name="myform">

<textarea name="description" form="myform" rows="3" cols="16"></textarea>
$message = $_POST["description"];

$body = "From: $name\r\n E-Mail: $email\r\n Address: $customer_address\r\n Phone: $phone\r\n Dog: $dog\r\n Breed: $breed\r\n Age: $age\r\n Description: $message";

 $mail->MsgHTML($body);

Like I said it sends all of the other vars except the $message (textarea on form). I am using the latest version of PHP mailer.

Jacob Herper
Jacob Herper
94,150 Points

Your HTML code is missing, can you post the HTML part again?

Yes, here it is now with html - I was missing the ticks originally.

2 Answers

Hi Michael,

Apologies for the first answer, I got a little trigger happy there!

Ok so... couple of things for the form. You don't need to reference the form in your HTML unless the input element is outside of the form. In your example it's inside: -

<form action="./index.php" method="post" id="myform">

<textarea name="description" rows="3" cols="16"></textarea>

<input type="submit" value="Send" />

</form>

If you do want to reference the form the form must be referenced by the ID attribute and not the name attr.

<form action="./index.php" method="post" id="myform">

and not

<form action="./index.php" method="post" name="myform">

Then you can use...

<textarea name="description" form="myform" rows="3" cols="16"></textarea>

Here's an example: -

http://www.kryptonite-dove.com/sandbox/tth/paccione-1

Could you elaborate a little on the line breaks issue? :)

Brilliant! Thank You!

Changed name to ID and yes Indeed it works perfectly now.

In regards to the line breaks...

Input

$body = "From: $name\r\n E-Mail: $email\r\n Address: $customer_address\r\n Phone: $phone\r\n Dog: $dog\r\n Breed: $breed\r\n Age: $age\r\n Description: $message";

Output Email

From: Test Name E-Mail: test@comcast.net Address: Phone: Test Phone Dog: Test Dog Name Breed: Test Breed Age: Test Age Description: Test Description

Preferred Output Email would have line breaks for each input field instead of it being on one line and wrapping in the received contact form email.

If you're viewing the output in your browser unless the output is wrapped in pre tags it will render all on one line. Try viewing source on the page and in the browser editor you should see new lines.

If you want to see new lines in the browser replace \r\n with <br />.

Hope that helps!