Well, for the verification, read the manual on [man]eregi/man and search for bob cop's User note. THat has a nearly perfect email validation (RFC compliant). But the regex should be:
^[-a-z0-9!#$%&\'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&\'*+/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){2,63}$
As for "requiring" each: It's a simple if(){}else{} statement:
if(empty($fname)){ echo 'First Name Required'; }
I think you might be able to work it from there....
So... putting it all together: (this is what I would do) 🙂
<?php
/**
* Define verification Function
*/
function verifyemail($email) {
if(eregi("^[-a-z0-9!#$%&\'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&\'*+/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){2,63}$", $email))
{
return TRUE;
}
else
{
return FALSE;
}
}
/***/
if(isset($_REQUEST['submit']))
{
// If the form is submitted:
$errors = array();
$e_present = FALSE; // Set default of No errors present
$validemail = TRUE;
$fields = array('fname'=>'Please fill in your first name',
'lname'=>'Please fill in your last name',
'company'=>'Please fill in your company name',
'address1'=>'Please fill in your address (line 1)',
'city'=>'Please fill in your city',
'state'=>'Please fill in your state',
'zipcode'=>'Please fill in your zipcode',
'phone'=>'Please fill in your phone number',
'email'=>'Please fill in your email address',
'program'=>'Please fill in the program'
);
foreach($_REQUEST as $key => $val)
{
if(array_key_exists($key, $fields))
{
// If the current REQUEST item is a field that we want:
if(empty($_REQUEST[$key]))
{
$e_present = TRUE;
$$key = '';
array_push($errors, $key);
}
else
{
if($key == 'email')
{
if(FALSE==verifyemail($_REQUEST['email']))
{
$e_present = TRUE;
$validemail = FALSE;
}
else
{
$email = $_REQUEST['email'];
}
}
else
{
$$key = $val;
}
}
}
else
{
if($key == 'address2')
{
$$key = $val;
}
}
}
if($e_present == FALSE)
{
// No errors present, send email
$to = ""; ## ENTER YOUR EMAIL ##
$subject = "You Have Received a New Enrollment Request! - Etiquette School of WA State";
$message = $fname." ".$lname." has filled out the enrollment form
on the site. As a result you are getting this email! Here's the info you'll probably want:\n\n
First Name: ".$fname."\n
Last Name: ".$lname."\n
Company: ".$company."\n
Address Line 1: ".$address1."\n
Address Line 2: ".$address2."\n
City: ".$city."\n
State: ".$state."\n
Zip Code: ".$zipcode."\n
Phone: ".$phone."\n
Email Address: ".$email."\n
Selected Program: ".$program."\n\n
Have a great day!<br><br>";
$fromname = $fname.' '.$lname;
$header = "MIME-Version: 1.0\n";
$header .= "Content-type: text/plain; charset=iso-8859-1\n";
$header .= "X-Priority: 3\n";
$header .= "X-MSMail-Priority: Normal\n";
$header .= "X-Mailer: php\n";
$header .= "From: \"".$fromname."\" <".stripslashes($email).">\n";
if(mail($to, $subject, $message, $header))
{
header( "Location: http://www.theetiquetteschoolofwashingtonstate.com/thanks.htm");
}
else
{
// Error sending the email
echo 'There was an error sending your email. Please try again later.';
}
}
else
{
// Show our form with listed errors at top:
echo '
<div style="width: 350px; height; auto; margin: 0; padding: 5px; border: 1px solid #900; background: #ee6363;">
<p style="width: 340px; margin: 0; padding: 0;">You have submitted information with errors. You need to fill out the following fields:</p>
<ul style="list-style-type: circle; font-weight: bold;">';
foreach($errors as $error)
{
echo '<li>'.$fields[$error].'</li>';
}
if($validemail==FALSE)
{
echo '<li>Please enter a valid email address</li>';
}
echo '</ul>
</div><br><br>';
// Show form:
$err_div = '<div style="background: #ee6363; font-weight: bold; border: 2px solid #900; padding: 5px 0; margin: 3px 0;">';
$form = '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
$form .= (in_array('fname', $errors))?$err_div:'';
$form .= 'First Name: <input type="text" name="fname" value="'.$_REQUEST['fname'].'">';
$form .= (in_array('fname', $errors))?'</div>':'<br>';
$form .= (in_array('lname', $errors))?$err_div:'';
$form .= 'Last Name: <input type="text" name="lname" value="'.$_REQUEST['lname'].'">';
$form .= (in_array('lname', $errors))?'</div>':'<br>';
$form .= (in_array('company', $errors))?$err_div:'';
$form .= 'Company: <input type="text" name="company" value="'.$_REQUEST['company'].'">';
$form .= (in_array('company', $errors))?'</div>':'<br>';
$form .= (in_array('address1', $errors))?$err_div:'';
$form .= 'Address Line 1: <input type="text" name="address1" value="'.$_REQUEST['address1'].'">';
$form .= (in_array('address1', $errors))?'</div>':'<br>';
$form .= 'Address Line 2: <input type="text" name="address2" value="'.$_REQUEST['address2'].'"><br>';
$form .= (in_array('city', $errors))?$err_div:'';
$form .= 'City: <input type="text" name="city" value="'.$_REQUEST['city'].'">';
$form .= (in_array('city', $errors))?'</div>':'<br>';
$form .= (in_array('state', $errors))?$err_div:'';
$form .= 'State: <input type="text" name="state" value="'.$_REQUEST['state'].'">';
$form .= (in_array('state', $errors))?'</div>':'<br>';
$form .= (in_array('zipcode', $errors))?$err_div:'';
$form .= 'Zipcode: <input type="text" name="zipcode" value="'.$_REQUEST['zipcode'].'">';
$form .= (in_array('zipcode', $errors))?'</div>':'<br>';
$form .= (in_array('phone', $errors))?$err_div:'';
$form .= 'Phone Number: <input type="text" name="phone" value="'.$_REQUEST['phone'].'">';
$form .= (in_array('phone', $errors))?'</div>':'<br>';
$form .= (in_array('email', $errors) || $validemail==FALSE)?$err_div:'';
$form .= 'Email: <input type="text" name="email" value="'.$_REQUEST['email'].'">';
$form .= (in_array('email', $errors) || $validemail==FALSE)?'</div>':'<br>';
$form .= (in_array('program', $errors))?$err_div:'';
$form .= 'Program: <input type="text" name="program" value="'.$_REQUEST['program'].'">';
$form .= (in_array('program', $errors))?'</div>':'<br>';
$form .= '<input type="submit" name="submit" value="Submit"> <input type="reset" value="Clear Fields">
</form>';
echo $form;
}
}
else
{
echo <<<HTML
<form action="{$_SERVER['PHP_SELF']}" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
Company: <input type="text" name="company"><br>
Address Line 1: <input type="text" name="address1"><br>
Address Line 2: <input type="text" name="address2"><br>
City: <input type="text" name="city"><br>
State: <input type="text" name="state"><br>
Zipcode: <input type="text" name="zipcode"><br>
Phone Number: <input type="text" name="phone"><br>
Email: <input type="text" name="email"><br>
Program: <input type="text" name="program"><br>
<input type="submit" name="submit" value="Submit"> <input type="reset" value="Clear Fields">
</form>
HTML;
}
The above script will give you a nice little form that when submitted, if errors are caught, will list them at the top. Then, it will also highlight around the offending input. Here are some screenshots:
Errors


Successful Email

Hope that helps you.
~Brett