title: Javascript contact form submit post
body: <p>I am trying to validate an email field and if valid submit my form. But the form is giving me an error when i submit it to the server. If i submit it with a submit button it works fine.</p>
  
  <pre><code>&lt;script&gt;
  
  function ValidateEmail(inputText)  
  {  
  var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
  if(inputText.value.match(mailformat))  
  {  
  document.form1.submit();
  }  
  else  
  {  
  alert("You have entered an invalid email address!");  
  document.form1.email.focus();  
  return false;  
  }  
  }  
  
  &lt;/script&gt;
  
  &lt;form action="mailer.php" id="form1"   name="form1" method="POST"&gt;
  
  
  
    &lt;input type="text"  size="19" name="name"/&gt;
  &lt;input type="text"  size="19" name="phone"/&gt;
  &lt;input type="text"  size="19" name="email"/&gt;
  
  
      &lt;a  onclick="ValidateEmail(document.form1.email)" href="#"&gt;Submit&lt;/a&gt;
  
  
  
  
  &lt;/p&gt;
  &lt;/form&gt;
  
  
  &lt;?php
  if(isset($_POST['submit'])) {
  
  $to = "marshal@smedia.ca"; 
  $to2 = $_POST['email'];
  $subject = "Lead Gen";
  $subject2 = "Coupon2";
  $first_field = $_POST['name'];
  $email_field = $_POST['email'];
  $phone_field = $_POST['phone'];
  $headers .= "Content-type: text/html\r\n"; 
  $headers .= 'From: Nurray GM &lt;welcome@mycompany.com&gt;' . "\r\n";
  
  
  $body = "
  
  From: $first_field \n   
  
  E-Mail: $email_field\n 
  
  Phone Number: $phone_field\n ";
  
  
  $body2 = "
  
  
  &lt;html&gt;
   &lt;body bgcolor='#DCEEFC'&gt;
      &lt;p&gt;Hello  $first_field,&lt;/p&gt; 
  
      &lt;p&gt;Thank you for applying for pre approval. balbalblah.&lt;/p&gt; 
  
    &lt;img src='www.smedia.ca/clients/leadgen/murray/coupon.jpg' width='500px' height='200px' /&gt;
  
  
    &lt;/body&gt;
  &lt;/html&gt; 
  
  ";
  
  
  mail($to, $subject, $body, $headers);
  
  mail($to2, $subject2, $body2, $headers );
  
  header("Location: http://www.smedia.ca/clients/leadgen/murray/redirect.html");  
  } else {
  echo "Sorry - something seems to be wrong. Your message was not sent";
  }
  
  
  ?&gt;
  </code></pre>