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

Ross Kendall-Selwyn
Ross Kendall-Selwyn
12,813 Points

Why does form submission does not fire on the first click but does after refreshing and clicking a second time?

I've built a contact form as a side project using PHP. When I complete the form and click submit it moves to the location specified: /index-contact.php?submission but I don't receive the email. If i refresh the page complete the form and click submit again, then i get BOTH emails at once. It's like the first email is waiting for the second email to push it through. Can anyone explain why this is happening?

I'm not quite sure how to add my code in the correct format but:

<?php

if (isset($_POST['submit'])) {

$name = $_POST['name']; $subject = $_POST['subject']; $mailFrom = $_POST['mail']; $phone = $_POST['phone']; $address = $_POST['address']; $body = $_POST['body']; $radio = $_POST['time'];

$mailTo = "myemail@email.com"; $headers = "From: ".$mailFrom; $txt = "You have recieved an enquiry from: ".$name.".\n\n Phone Number: ".$phone."\n\n Address: ".$address."\n\n Message: ".$body."\n\n Time to call: ".$radio;

mail($mailTo, $subject, $txt, $headers); header("Location: index-contact.php?submission"); }

?>

The HTML markup is generic form markup with a submit button, no AJAX, jQuery etc.