title: PHP Contact Form - form getting replaced by a message?
body: <p>I recently started to learn PHP and am having problem with a contact form.</p>
  
  <p>The problem with the form is that it doesn't send an email and echoes the error message below the form even before I send it.
  Sometimes I get it to work and then it breaks again.</p>
  
  <p>Also, I would like the message "Message sent!" to replace the whole contact form when it is sent. How do I make that happen without linking to another page?</p>
  
  <p>The code is the following:</p>
  
  <pre><code>&lt;form method="POST" action="index.php"&gt;
      &lt;input name="name" type="text" placeholder="Name"&gt;
      &lt;input name="email" type="email" required placeholder="Email"&gt;
      &lt;input name="subject" type="text" placeholder="Subject"&gt;
      &lt;textarea name="message" rows="15" required placeholder="Message"&gt;&lt;/textarea&gt;
      &lt;input name="submit" type="submit" value="Send"&gt;
  &lt;/form&gt;
  
  &lt;?php
      if(isset($_POST['submit'])) 
      {
      $name_field=$_POST['name'];
      $email_field=$_POST['email'];
      $subject_field=$_POST['subject'];
      $message_field=$_POST['message'];
      $to="example@outlook.com";
      $from="example@outlook.com";
      $subject="Contact Form Message";
      $body="Name: $name_field\n Email: $email_field\n Subject: $subject_field\n Message:\n $message_field";
      mail($to,$subject,$body,$from);
      echo "&lt;p&gt;Message sent!&lt;/p&gt;"; 
      } 
      else
      {
      echo "&lt;p&gt;An error occured. Please try again.&lt;/p&gt;";
      }
  ?&gt;
  </code></pre>
  
  <p>Thank you very much for any help whatsoever.
  Also, if you have any idea how to make the code better, cleaner or more efficient, please do tell!</p>