Contact form

Hi,

Have a simple contact form, can anyone help me with adding validation so that all fields have to be filled out before the mail sends please?

I have this

     <?php
if(isset($_POST['submit'])) {

$to = "me@myemail.com";
$subject = "Enquiry";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];

  $headers = "MIME-Version: 1.0\
";
  $headers .= "Content-type: text/plain; charset=iso-8859-1\
";
  $headers .= "From: Site User <someone@yoursite.com> \
";
  $headers .= "X-Mailer: PHP's mail() Function\
";

 
$body = "From: $name_field\
 E-Mail: $email_field\
 Message:\
 $message";

echo "Your email has been sent!";
mail($to, $subject, $body, $headers);

} else {

echo "blarg!";

}
?>
   <form method="POST" action="mailer.php">
    
    <label for="name">Your Name:</label><br />     
   <input type="text" name="name" size="26" /><br /><br />
   
   <label for="email">Your Email:</label><br />
   <input type="text" name="email" size="26" /><br /><br />

   <label for="message">Your Message:</label><br />
   <textarea rows="10" name="message" cols="50" ></textarea>
   <br />
   <br />
   <input type="submit" value="Submit" name="submit" class="dark" />
</form>

How would I do an If then else statement for this?

The contact form code in the link below has validations and other features built-in
Creating a contact form for your website

Well before using mail function you can add a if statement. You must check the lenght for name and message, and you can find over the internet a regular expresion for email to check it. At the beginning of if statement you could add an empty string variable. If on any validation step you get a false then concatenate it with an error text message. Finally you do another check: if string is empty then send email, else show form again with error string.