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

Jeremy Coleman
Jeremy Coleman
5,449 Points

Contact Form to Email

I am in need of a little direction, I don't have any code off hand I am just wanting a pointer on how I can learn how to make anything that gets submitted from a form to email.

I have gone over courses that teach PHP but still don't understand how the server exactly sends anything from the form.

Thanks in advance.

Simon Coates
Simon Coates
28,694 Points

treehouse has a HTTP course that might help you. Covering the html form courses would give you more form options. The PHP just recieves the HTTP request and returns a HTTP response (usually just html). Understanding how the html form translates into a HTTP request and how that translates into PHP ($_GET, $_POST superglobals) is a good knowledge to have. Getting it is about understanding the PHP in context.

(there are a couple things the PHP course probably doesn't' teach you. It probably skips how to recieve some form elements from HTML - in some cases you have to use an array naming convention on your form elements. I think the course treats response as HTML, obscuring the HTTP nature of what is occurring. For example, the course doesn't cover returning non 200 status codes using header. I think the course also skips cookies, sessions. SO THE NOTION THAT SOME INFORMATION IS MISSING IS CORRECT. I get this stuff because i've taken taken lynda, pluralsight and treehouse courses on PHP and HTTP. Courses will omit some content.)

Jeremy Coleman
Jeremy Coleman
5,449 Points

So technically I could use any programming language to achieve this goal?

Simon Coates
Simon Coates
28,694 Points

There are options for backend programming languages, eg. java. php, python, ruby, and javascript (i still regard javascript on the back end as being comparatively exotic). And these have various frameworks built on top of them.

The PHP courses do include sending an email off a form, but they do skip details that would be useful in real world use. They just don't want to deal with the complexity of ports, smtp etc. Sitepoint has a tutorial on email with php that might help.

I actually misunderstood your question, so I apologize if my earlier response wasn't helpful. For whatever reason, i thought you were struggling with getting your form data into PHP, not with the PHP email code.

1 Answer

I'm not sure if this is the angle you're looking for; but what you can do is set PHP variables to the values posted from your HTML form, i.e. $first_name = filter_input(INPUT_POST, 'first_name'); --- that variable and any other variable you assign are now allowed to be included in the subject or body of an email that you send on the PHP page where your HTML <form action> takes place. The PHP Mailer (found on GitHub) is extremely simple with very minor configuration required. The subject and body work the same as an echo statement would with the same syntax rules.

Is this the direction you want to go with this question?

Jeremy Coleman
Jeremy Coleman
5,449 Points

I understand how to link the PHP to the form, I will watch some more videos on here as Simon suggested, but my confusion is what happens when a user clicks submit. I previously followed the tree house tutorial with Mike the Frog and the form wouldn't submit an email. Which is what I am trying to figure out.

Jeremy Coleman
Jeremy Coleman
5,449 Points

I understand how to link the PHP to the form, I will watch some more videos on here as Simon suggested, but my confusion is what happens when a user clicks submit. I previously followed the tree house tutorial with Mike the Frog and the form wouldn't submit an email. Which is what I am trying to figure out.

Simon Coates
Simon Coates
28,694 Points

ok, so create a form with the various inputs you want. set action and method attributes, to tell the form whether to GET or POST and where to send the data. If you're pointing it to an active .PHP file, the form values should be available in the .php script via the $_GET or $_POST, with the names you assigned the form (probably input) elements. eg. An input element with name of emailAddress, should be available via $_POST['emailAddress'] if you used the post method (the HTTP videos i mentioned covered this).

The PHP track covers the form submit and a couple additional pieces (redirect to protect against refresh performing the action twice, and the use of a honeypot as a primitive attempt to stop bots abusing your form). A common form pattern is covered (the sticky form).