View Single Post
  #2 (permalink)  
Old 06-30-05, 05:31 AM
Vulture Vulture is offline
Newbie Coder
 
Join Date: Dec 2004
Location: Scotland, UK
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
There are numerous ways to check for required fields. Your also not limited to PHP, JavaScript can perform these checks before the form is submitted. However, here is one way you could do it:
PHP Code:

<?php

if (($_POST) and (!empty($_POST['name'])) and (!empty($_POST['email'])) and (!empty($_POST['message']))) {
    if (
get_magic_quotes_gpc()) {
        foreach (
$_POST as $key => $value) {
            
$temp stripslashes($value);
            
$_POST[$key] = $temp;
        }
    }
    
$to 'info@domain.com';
    
$subject 'CEC website contact form';

    
// message goes here
    
$message "Full Name: " $_POST['name'] . "\n";
    
$message .= "Email: " $_POST['email'] . "\n";
    
$message .= "Telephone: " $_POST['phone'] . "\n";
    
$message .= "Message: " $_POST['message'] . "\n";

    
// headers go here
    
$headers "From: " $_POST['email'] . "\n";
    
$headers .= "Content-type: text/plain; charset=UTF-8";

    
//send mail
    
$sent mail($to$subject$message$headers);
    
$autorespond mail($_POST['email'], "Form Auto Response.""Thank you for contacting me, your message has been received and we will contact you shortly.""From: Me <info@domain.com>");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>
<title>Title</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<h1>Contact</h1>

<?php
if (isset($sent)) {
    echo 
'<p><span class="thanks"><strong>Thank you</strong> for contacting us. Your message will be responded to as soon as possible.</span></p>';
}
else if ((empty(
$_POST['name'])) or (empty($_POST['email'])) or (empty($_POST['message']))) {
    echo 
'<p>Please ensure you have filled out all the required fields</p>';
}
?>

<div id="form">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<fieldset>
<legend>Personal information</legend>

<span class="formLabel"><label for="name">Full Name:</label><br /></span>
<input class="inputfield" type="text" id="name" name="name" size="30" />

<span class="formLabel"><label for="email">Email:</label><br /></span>
<input class="inputfield" type="text" id="email" name="email" size="30" />

<p><span class="formMessage"><label for="message">Message:</label><br /></span>
<textarea class="inputarea" id="message" name="message" rows="5" cols="30"></textarea></p>

</fieldset>

<p><input type="submit" class="inputsend" name="Submit" value="Send Form" /></p>

</form></div><!-- end #form -->
</body>
</html>
The changes I've made are pretty straight forward. At the first if statement I've added on a check to see whether the fields are empty or not. If they are, the entire send mail block is skipped. I then perform a similar check in the later part of the code, which outputs an error if any of these fields are empty.

Hope that helps
Reply With Quote