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"] . "\\r\
";

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

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




        $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

Essentially:


<?php

if( isset($_POST['formsent']) ){

var_dump( $_POST );

if( empty($_POST[''name]) ) $error['name'] = true ;
// do your other stuff here

}
?>

<form action=""  method="POST" name="contact" id="contact">
<p><strong>Name:*</strong><br />
<input name="name" type="text" class="ctextField" /> <?php if ($error['name']) echo " *** Please add your name"?></p>
<input type = hidden name="formsent" value=1>
<input type="submit">
</form>

When you do this for your entire form though, you might think of better ways of doing it and making it less error prone (eg using arrays).

Hi Cups,

That’s awesome, thank you for that!

I added the PHP at the top of my form and Dreamweaver seemed to display a syntax error for:

if( empty($_POST[''name]) ) $error['name'] = true ;

I tried a couple of things, and changed it to…

if( empty($_POST['name']) ) $error['name'] = true ;

…which seemed to be ok.

But when you hit Submit, the form still goes through and just leaves the field blank on the email I get sent.

This is what my PHP looks like now:



<?php

if( isset($_POST['formsent']) ){

var_dump( $_POST );

if( empty($_POST['name']) ) $error['name'] = true ;
if( empty($_POST['email']) ) $error['email'] = true ;
if( empty($_POST['message']) ) $error['message'] = true ;
// do your other stuff here

}



if (array_key_exists('submit', $_POST)) {

    $name = $_POST['name'];

    $email = $_POST['email'];

    $telephone = $_POST['telephone'];

    $company = $_POST['company'];

    $address1 = $_POST['address1'];

    $address2 = $_POST['address2'];

    $town = $_POST['town'];

    $county = $_POST['county'];

    $postcode = $_POST['postcode'];

    $formMessage = $_POST['message'];





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



$to = "me@mydomain.com";

$subject = "Contact from website";



$message = $headers;

$message .= "Name: " . $_POST["name"] . "\\r\
";

$message .= "E-mail: " . $_POST["email"] . "\\r\
";



$headers  = "MIME-Version: 1.0\\r\
";

$headers .= "Content-type: text/html; charset=iso-8859-1\\r\
";









        $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;'>".$formMessage."</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' ) ;

}



?>


Do you have any ideas what may be causing the problem?

Thanks again for your help :slight_smile:

SM

As I wasn’t able to previously test the code I posted and it contained errors, here is a better version (tested this time).

This is just to show how to push a message back to your in-completed form, making better use of PHP arrays - I am not dealing with any problems your email script has thrown up.

<?php

$error = array();

$basic_form_elements = array(
'name', 
'address_line_1', 
'address_line_2', 
'tel', 
'mob', 
'fax', 
'inside_leg', 
);

if( isset($_POST['formsent']) ){

    if( empty($_POST['name']) ) {

    $error['name'] = " *** Please add your name" ;

    }else{

    // do your other stuff here, send mail etc
    var_dump( $_POST );
    exit();

    }
}
?>

<form action=""  method="POST" name="contact" id="contact">


<?php foreach($basic_form_elements as $el) : ?>

    <p><strong><?php echo ucwords(str_replace("_", " ", $el)) ?></strong><br />
    <input name="<?php echo $el ?>" type="text" 
        class="ctextField" value="<?php if(isset($_POST[$el])) echo htmlentities($_POST[$el])?>"/> 
    <?php echo (array_key_exists($el ,$error)) ?  $error[$el] : "" ;?></p>

<?php endforeach;?>


<input type = "hidden" name="formsent" value="1">
<input type="submit">
</form>

You can then go on and have the basic_form_elements array contain the error message then you could simplify the error checking so that if count($errors) is > 0 then re-display the form. I hope I am not making things too convoluted for you, but arrays are a cornerstone of programming - and you’d do well to spend a good time investigating how best to use them.