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

Ryan Sherry
Ryan Sherry
12,449 Points

Why is my PHP Contact form refreshing upon submission and not sending an email? Why do success/fail messages not pop up?

Here is a snapshot of my workspace:

https://w.trhou.se/9mfsoawfhr

I created a contact form that is supposed to send an email upon submission provided that the spam question is answered correctly.

However, upon submission, the screen appears to refresh and I do not get an email. Furthermore, neither the success nor error messages are displayed indicating whether it was sent or not.

I am newer to coding so I would not be surprised if I made a newbie mistake.

Thank you for any insight you may be able to provide.

Ryan

2 Answers

Jeff Lemay
Jeff Lemay
14,268 Points
  1. I think you'd want the php for the form processing to be at the top of the document (above the doctype). That way that code will run first (set your error messages, process the form, etc.) before you print any html to the page.
  2. You do not need to declare an action on the form if you are processing the form in the same file (index.php). You only need the method="post"
Ryan Sherry
Ryan Sherry
12,449 Points

Thank you Jeff! I don't have time to test this but will test it later today.