title: Contact form error messages on same page
body: <p>I have a working php code but when the user clicks "Send" it takes him to an empty page saying "thanks for sending" or it shows an error. Here my php code and html form. l want error and success text to show above first input. I tried it with iframe and it worked, but I can't change the font color so I can't really use it. Form is in an HTML file called inquiry.html and PHP is in inquiry.php</p>
  
  <pre><code>&lt;form method="post" action="inquiry.php"&gt;
  
      &lt;h2&gt;CONTACT US&lt;/h2&gt;
  
      &lt;p&gt;We are here to answer any questions you may have about our company. Reach out to us and we'll respond as soon as
          we can. &lt;/p&gt;
  
      &lt;input id="input" name="name" placeholder="Your Name"&gt;
  
      &lt;input id="input" name="email" type="email" placeholder="Your Email"&gt;
  
      &lt;input id="input" name="subject" type="subject" placeholder="Subject"&gt;
  
      &lt;textarea id="textarea" name="message" placeholder="Your Message"&gt;&lt;/textarea&gt;
  
      &lt;input id="submit" name="submit" type="submit" value="Send"&gt;
  
  &lt;/form&gt;
  
  &lt;?php
  
      if(isset($_POST['email'])) {                     
  
          // EDIT THE 2 LINES BELOW AS REQUIRED
  
          $email_to = "leonstopar1@gmail.com";
  
          $email_subject = $_POST['subject'];;
  
  
          function died($error) {
  
              // your error code can go here
  
              echo "We are very sorry, but there were error(s) found with the form you submitted. ";
  
              echo "These errors appear below.&lt;br /&gt;&lt;br /&gt;";
  
              echo $error."&lt;br /&gt;&lt;br /&gt;";
  
              echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
  
              die();         
          }
  
          // validation expected data exists
  
          if(!isset($_POST['name']) ||
  
              !isset($_POST['email']) ||
  
              !isset($_POST['subject']) ||
  
              !isset($_POST['message'])) {
  
              died('We are sorry, but there appears to be a problem with the form you submitted.');       
  
          }         
  
  
          $name = $_POST['name']; // required
  
          $email_from = $_POST['email']; // required
  
          $subject = $_POST['subject']; // not required
  
          $message = $_POST['message']; // required
  
  
          $error_message = "";
  
          $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  
        if(!preg_match($email_exp,$email_from)) {
  
          $error_message .= 'The Email Address you entered does not appear to be valid.&lt;br /&gt;';
  
        }
  
          $string_exp = "/^[A-Za-z .'-]+$/";
  
        if(!preg_match($string_exp,$name)) {
  
          $error_message .= 'The Name you entered does not appear to be valid.&lt;br /&gt;';
  
        }
  
        if(strlen($message) &lt; 2) {
  
          $error_message .= 'The Message you entered do not appear to be valid.&lt;br /&gt;';
  
        }
  
        if(strlen($error_message) &gt; 0) {
  
          died($error_message);
  
        }
  
  
          function clean_string($string) {
  
            $bad = array("content-type","bcc:","to:","cc:","href");
  
            return str_replace($bad,"",$string);
  
          }
  
  
          $email_message .= "Name: ".clean_string($name)."\n";
  
          $email_message .= "Email: ".clean_string($email_from)."\n";
  
          $email_message .= "Message: \n\n".clean_string($message)."\n";
  
  
      // create email headers
  
  
      $headers = "MIME-Version: 1.0" . "\r\n";
      $headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
      $headers .= "From: ".$name."\r\n" .
      "Reply-To: ".$name."\r\n" .
      "X-Mailer: PHP/" . phpversion();
  
      @mail($email_to, $email_subject, $email_message, $headers);  
  
  ?&gt;
  
  
  &lt;!-- include your own success html here --&gt;
  
  
  Thank you for contacting us. We will be in touch with you very soon.
  
  
  
  &lt;?php
  
  ?&gt;
  </code></pre>