Need help with PHP contact form

Hi guys,

I’ve made a PHP contact form for my site and need help with a couple of things:

The form action links an external PHP script (scripts/contact-form-script.php) but is there a way I can have it so the PHP script for the form is contained within the same PHP file as my contact form (contact.php)?

I tried just putting the form code at the top of contact.php but the browser automatically reads the anti-spam re-direct, so maybe that needs revising too?

The second thing is, how can I make the Name, Email and Message fields mandatory? So if a user tries to submit the form and hasn’t filled in one of the required fields and clicks submit, contact.php reloads with a message at the top of the form saying something like ‘Complete the required fields’ and highlights the relevant field with a red border?

Here’s the code for contact.php:

<form action="http://www.mydomain.com/scripts/contact-form-script.php" method="post" name="contact" id="contact">
<p><strong>Name:*</strong><br />
<input name="name" type="text" class="ctextField" /></p>
<p><strong>E-mail:*</strong><br />
<input name="email" type="text" class="ctextField" /></p>
<p><strong>Telephone:</strong><br />
<input name="telephone" type="text" class="ctextField" /></p>
<p><strong>Company:</strong><br />
<input name="company" type="text" class="ctextField" /></p>
<p><strong>Address:</strong><br />
<input name="address1" type="text" class="ctextField" /></p>
<p><input name="address2" type="text" class="ctextField" /></p>
<p><strong>Town:</strong><br />
<input name="town" type="text" class="ctextField" /></p>
<p><strong>County:</strong><br />
<input name="county" type="text" class="ctextField" /></p>
p><strong>Postcode:</strong><br />
<input name="postcode" type="text" class="ctextField" /></p>
<p><strong>Message:*</strong><br />
<textarea name="message" cols="55" rows="8" class="ctextField"></textarea></p>
<p><input name="submit" value="SEND MESSAGE" class="submitButton" type="submit" /><div style="visibility:hidden; width:1px; height:1px"><input name="url" type="text" size="45" id="url" /></div></p>
</form>

And this is the PHP I’m using to submit the form data for contact-form-script.php:


<?php



$headers .= "Reply-To: " . $_POST["email"] . "
";

$to = "me@mydomain.com";
$subject = "Contact from website";

$message = $headers;
$message .= "Name: " . $_POST["name"] . "
";
$message .= "E-mail: " . $_POST["email"] . "
";




        $message= '
   
   
            <table cellspacing="0" cellpadding="8" border="0" width="500">
            <tr>
                <td colspan="2"></td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td width="154" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td>
              <td width="314" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$name.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>E-mail address:</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$email.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Telephone number:</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$telephone.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Company:</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$company.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$address1.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$address2.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Town</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$town.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>County</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$county.'</td>
            </tr>
            <tr bgcolor="#eeeeee">
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Postcode</strong></td>
              <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$postcode.'</td>
            </tr>
           
            <tr bgcolor="#eeeeee">
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Message</strong></td>
            </tr>               
            <tr bgcolor="#eeeeee">
                <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$message.'</td>
            </tr>               
                                   
            <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr>
         </table>


           

';







$url = stripslashes($_POST["url"]);
if (!empty($url)) {
header( 'Location: http://www.go-away-spam-robots.com' );
exit();
} 

mail($to, $subject, $message, $headers);

header( 'Location: http://www.mydomain.com/sent.php' ) ;
?>

Any help on this would be greatly appreciated.

Thank you and I hope to hear from you!

SM