title: php mail() sending mail before form is filled
body: <p>I have a self posting document that has form fields to fill out with php processing on the same document. My problem is that upon opening the page (website), the message "Message Sent!" shows up immediately before the form can be filled out with information. The php mail() function is linked to my email account so I get the form data email. But no data is sent because the email was sent before the form could be filled out. I want to be able to fill out the form before the email is sent off so that way the form sends actual information. Ive researced this topic and came up with nothing. Any help would be awesome! Here's my code...</p>
  
  <pre><code>&lt;?php
      foreach($_POST as $key =&gt; $value)       //This will loop through each name-value in the $_POST array
      {
          $tableBody .= "&lt;tr&gt;";               //formats beginning of the row
          $tableBody .= "&lt;td&gt;$key&lt;/td&gt;";      //dsiplay the name of the name-value pair from the form
          $tableBody .= "&lt;td&gt;$value&lt;/td&gt;";    //dispaly the value of the name-value pair from the form
          $tableBody .= "&lt;/tr&gt;";              //End this row
      } 
  
      echo "&lt;table border='1'&gt;";
      echo "&lt;tr&gt;&lt;th&gt;Field Name&lt;/th&gt;&lt;th&gt;Value of field&lt;/th&gt;&lt;/tr&gt;";
      foreach($_POST as $key =&gt; $value)
      {
          echo '&lt;tr class=colorRow&gt;';
          echo '&lt;td&gt;',$key,'&lt;/td&gt;';
          echo '&lt;td&gt;',$value,'&lt;/td&gt;';
          echo "&lt;/tr&gt;";
      } 
      echo "&lt;/table&gt;";
      echo "&lt;p&gt;&amp;nbsp;&lt;/p&gt;";
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  
      &lt;style&gt;
  
      body {
          background-image: url("rbGrid.png");
          background-size: 150%;
          background-repeat: no-repeat;
          text-align: center;
      }
  
      div {
          background-color: black;
          opacity: 0.9;
          color: white;
          text-align: center;
      }
  
      h1 {
          color: white;
      }
  
      h2 {
          color: white;
      }
  
      #borderStyle {
          border: double thick red;
          border-radius: 45px;
          width: 50%;
          margin: 0 auto;
      }
  
      #hiddenStuff {
          display: none;
      }
  
      textarea {
          display: none;
          margin: 0 auto;
      }
  
      #mailingInformation {
          display: none;
          margin: 0 auto;
      }
  
      table {
          margin: 0 auto;
          border: solid thick red;
          border-radius: 20px;
      }
  
      th {
          border: solid thick red; 
          border-radius: 45px;
      }
  
      tr {
          color: white;
          border: solid thin red;
          border-radius: 45px;
      }
  
      td {
          color: white;
          border: solid thin red;
          border-radius: 45px;
      }
  
  
  &lt;/style&gt;
  
  &lt;script&gt;
  
      function showProductProblemComments()
      {
          document.getElementById("textarea").style.display = "block";
      }
  
      function showMailingListForm()
      {
          document.getElementById("mailingInformation").style.display = "block";
      }
  &lt;/script&gt;
  &lt;/head&gt;
  
  
  &lt;body&gt;
  
  &lt;?php
      $toEmail = "robinjennings@nephilim42.com";      //CHANGE within the quotes. Place email address where you wish to send the form data. 
                                          //Use your DMACC email address for testing. 
  
  
      $subject = "WDV341 Email Example";  //CHANGE within the quotes. Place your own message.  For the assignment use "WDV101 Email Example" 
  
      $fromEmail = "robinjennings@nephilim42.com";        //CHANGE within the quotes.  Use your DMACC email address for testing OR
                                          //use your domain email address if you have Heartland-Webhosting as your provider.  
  
  //   DO NOT CHANGE THE FOLLOWING LINES  //
  
      $emailBody = "Form Data\n\n ";          //stores the content of the email
      foreach($_POST as $key =&gt; $value)       //Reads through all the name-value pairs.   $key: field name   $value: value from the form                                  
      {
          $emailBody.= $key."=".$value."\n";  //Adds the name value pairs to the body of the email, each one on their own line
      } 
  
      $headers = "From: $fromEmail" . "\r\n";             //Creates the From header with the appropriate address
  
      if (mail($toEmail,$subject,$emailBody,$headers))    //puts pieces together and sends the email to your hosting account's smtp (email) server
      {
          echo("&lt;p&gt;Message successfully sent!&lt;/p&gt;");
      } 
      else 
      {
          echo("&lt;p&gt;Message delivery failed...&lt;/p&gt;");
      }
  
      /*$inName = $_POST["Name"];     
      $inEmail = $_POST["Email Address"];
      $inAddress = $_POST["address"];
      $inReason = $_POST["Reason"];
      $inComments = $_POST["comments"];
      $inMailBox = $_POST["Mailing List"];
      $inUseAddress = $_POST["checkForAddress"];
      $inFirstName = $_POST["mailingName"];
      $inLastName = $_POST["mailingLastName"];
      //$inMailingAdd $_POST["mailingAddress"];
      $inPhoneNumber = $_POST["phoneNumber"];
      $inMoreInfo = $_POST["More Info"];*/
  ?&gt;
  
  &lt;h1&gt;WDV341 Intro PHP&lt;/h1&gt;
  &lt;h2&gt;Programming Project - Contact Form&lt;/h2&gt;
  
  &lt;div&gt;
  &lt;form name="form1" method="POST" action="contactForm2.php"&gt;
    &lt;p&gt;&amp;nbsp;&lt;/p&gt;
    &lt;p&gt;
  &lt;div id = "borderStyle"&gt;
      &lt;label&gt;Your Name:
        &lt;input type="text" name="Name" id="textfield" required&gt;
    &lt;/p&gt;
  &lt;br&gt;&lt;br&gt;
    &lt;p&gt;Your Email: 
      &lt;input type="text" name="Email Address" id="textfield2" required&gt;
    &lt;/p&gt;
  &lt;br&gt;&lt;br&gt;
    &lt;p&gt;Your Address:
      &lt;input type = "text" name = "address" id = "living"&gt;
    &lt;/p&gt;
  &lt;br&gt;&lt;br&gt;
    &lt;p&gt;Reason for contact: 
        &lt;select name="Reason" id="select2" onChange = "showProductProblemComments()" required&gt;
          &lt;option value="default"&gt;Please Select a Reason&lt;/option&gt;
          &lt;option value="product"&gt;Product Problem&lt;/option&gt;
          &lt;option value="return"&gt;Return a Product&lt;/option&gt;
          &lt;option value="billing"&gt;Billing Question&lt;/option&gt;
          &lt;option value="technical"&gt;Report a Website Problem&lt;/option&gt;
          &lt;option value="other"&gt;Other&lt;/option&gt;
        &lt;/select&gt;
    &lt;/p&gt;
  &lt;br&gt;&lt;br&gt;
    &lt;p&gt;Comments:
        &lt;textarea name="comments" id="textarea" cols="45" rows="5"required&gt;&lt;/textarea&gt;
    &lt;/p&gt;
  &lt;br&gt;&lt;br&gt;
    &lt;p&gt;
        &lt;input type="checkbox" name="Mailing List" id="checkbox" onClick = "showMailingListForm()"&gt;
        Please put me on your mailing list.
    &lt;/p&gt;
  &lt;div id = "mailingInformation"&gt;
  &lt;h3&gt;Please fill out the form below to be put on the mailing list to recieve coupons and special offers&lt;/h3&gt;
    &lt;p&gt;Check the box to use address above
    &lt;input type = "checkbox" name = "checkForAddress" id = "checkAddress"&gt;
    &lt;/p&gt;
    &lt;p&gt;First Name:
          &lt;input type = "text" name = "mailingName" id = "mailing"&gt;
    &lt;/p&gt;
    &lt;p&gt;Last Name:
          &lt;input type = "text" name = "mailingLastName" id = "mailingLast"&gt;
    &lt;/p&gt;
    &lt;p&gt;Mailing Address:
          &lt;input type = "text" name = "mailingAddress" id = "mailingAdd"&gt;
    &lt;/p&gt;
    &lt;p&gt;Phone Number(Optional)
          &lt;input type = "text" name = "phoneNumber" id = "phone"&gt;
    &lt;/p&gt;
  &lt;/div&gt;
    &lt;p&gt;
        &lt;input type="checkbox" name="More Info" id="checkbox2"&gt;
        Send me more information about your products.&lt;/label&gt;
      &lt;/p&gt;
  &lt;br&gt;&lt;br&gt;
    &lt;p&gt;
      &lt;input type="hidden" name="hiddenField" id="hidden" value="application-id:US447"&gt;
    &lt;/p&gt;
  
  &lt;br&gt;&lt;br&gt;
  
    &lt;p&gt;
      &lt;input type="submit" name="button" id="button" value="Submit"&gt;
      &lt;input type="reset" name="button2" id="button2" value="Reset"&gt;
    &lt;/p&gt;
  &lt;div&gt;
  &lt;/form&gt;
  &lt;div id = "hiddenStuff"&gt;
  &lt;p&gt;
      &lt;table border='a'&gt;
      &lt;tr&gt;
          &lt;th&gt;Field Name&lt;/th&gt;
          &lt;th&gt;Value of Field&lt;/th&gt;
      &lt;/tr&gt;
      &lt;?php echo $tableBody;  ?&gt;
      &lt;/table&gt;
  &lt;!--&lt;/p&gt;
  &lt;p&gt;Name: &lt;?php echo $inName; ?&gt;&lt;/p&gt;
  &lt;p&gt;Email: &lt;?php echo $inEmail; ?&gt;&lt;/p&gt;
  &lt;p&gt;Address: &lt;?php echo $inAddress; ?&gt;&lt;/p&gt;
  &lt;p&gt;Reason: &lt;?php echo $inReason; ?&gt;&lt;/p&gt;
  &lt;p&gt;Comments: &lt;?php echo $inComments; ?&gt;&lt;/p&gt;
  &lt;p&gt;Mailing List:  &lt;?php echo $inMailBox; ?&gt;&lt;/p&gt;
  &lt;p&gt;Use Previous Address Given: &lt;?php echo $inUseAddress; ?&gt;&lt;/p&gt;
  &lt;p&gt;First Name: &lt;?php echo $inFirstName; ?&gt;&lt;/p&gt;
  &lt;p&gt;Last Name?: &lt;?php echo $inLastName; ?&gt;&lt;/p&gt;
  &lt;p&gt;Mailing Address: &lt;?php echo $inMailingAdd; ?&gt;&lt;/p&gt;
  &lt;p&gt;Phone Number: &lt;?php echo $inPhoneNumber; ?&gt;&lt;/p&gt;
  &lt;p&gt;More Information: &lt;?php echo $inMoreInfo; ?&gt;&lt;/p&gt;--&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  
  
  
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Some line of code have been commented out for the sake of experimenting. Thank you in for the help.</p>