Contact form submitting twice to email

2.4k Views Asked by At

I have a basic contact form that is sending the user's message twice to the company that has displayed the form: once with all of the correct info and once with the text fields blank, i.e. name, email, phone, message are listed but contain no information. I've been working on it for hours and cannot figure it out.

At the very top of my page I have this header:

 <?php
 ob_start();
 session_start();
 if( isset($_POST['submit'])) {
 if( $_SESSION['security_code'] == $_POST['security_code'] &&  !empty($_SESSION['security_code'] ) ) {
  // Processing
 header("Location:http://www.berrieswebdesign.com/business1 /thankyou.php?happymessage=thanks");
 unset($_SESSION['security_code']);

 } else {
 // Error Message
 header("Location:http://www.berrieswebdesign.com/business1 /thankyou.php?sadmessage=securitybreach");
    unset($_POST['submit']);

  }

  } else  {

  }
ob_flush();
 ?> 

Javascript:

 $(function(){
 $('#contact').validate({
 submitHandler: function(form) {
 $(form).ajaxSubmit({
 url: 'process.php',
 success: function() {
 $('#contact').hide();
 $('#contact-form').append("<p class='thanks'>Thanks! Your request has been sent.</p>")
 }
 });
 }
 });         
 });

and process.php

 function GetHeaders()
 {
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    // Additional headers
    $headers .= 'From: Company Name<[email protected]>' . "\r\n";
    return $headers;
 }
 // Get Data    
 $name = strip_tags($_POST['name']);
 $email = strip_tags($_POST['email']);
 $phone = strip_tags($_POST['phone']);
 $message = strip_tags($_POST['message']);

 // Send Message
    $headers = GetHeaders();
    $intro = "\"Thank you for contacting company name. We are very interested in assessing your situation and will be in touch as soon possible.\" <br />
    <br/>
    Best Regards,<br/>
    <br/>
    Company<br/>
    ";
 mail($email, "RE: Contact Form Submission", $intro, $headers);
 mail("[email protected]", "Inquiry", "Name: {$name}\n Email: {$email}\n Phone: {$phone}\n Message: {$message}\n");

 ?>

Thanks for any help in advance.

2

There are 2 best solutions below

2
On

Edit: Based on the additional javascript code you added to your question...

The problem could be that the form is submitted by both javascript and the html. If you removed the javascript, the form may still submit. That indicates that javascript is not required to submit the form. However, if javascript is on, then ajaxSubmit will run, but that does not stop the form from submitting normally as well. If you want to stop the form from submitting, you need to say something like:

return false; on the submitHandler.


You could have that PHP code run whether or not a form has been submitted, and the code is on the same webpage as the form is on.

That would mean that an email is sent whenever the user first displays the form, as well as after the user submits the form.

To stop this, you can:

  • make the form submit to a different webpage, and move the PHP code which sends the email to that webpage.
  • Or you can set up an if guard, eg. if ($name + $email + $phone + $message != "") { /* send mail here */}. The if (...) condition checks if any data has been entered.
2
On

You have two calls to your mail function. It should only be called once and look similar to this:

mail($to, $subject, $message, $headers);