Hey guys, I have a PHP form that I'm working on -- actually, it's fully functional -- but I feel like I might be going about certain ideas all wrong. It goes like this: the contact.php page has the PHP at the top and then the HTML below. When you arrive at the page it shows the form; when you submit the form, the page posts to itself; if you made errors, it redirects to an error page...otherwise, it displays a success message on the page. My question is: which of these arrays, if statements, etc. could be reworked to be simpler or more correct? I really feel like the line for checking blank fields
if ($submitted == true) {
if($err == true || $name == '' || $message == '' || $recipient == 'please_select') {
header("location:/clients/hollybrook/error");
exit();
}
}
is not done well. Here is the entire page (I removed a lot of the HTML for brevity. doctype is strict):
<?php
$name = htmlspecialchars($_POST["name"], ENT_QUOTES);
$phone = htmlspecialchars($_POST["phone"], ENT_QUOTES);
$emailaddress = trim($_POST["email"]);
$recipient = htmlspecialchars($_POST["recipient"], ENT_QUOTES);
$message = stripslashes(htmlspecialchars($_POST["message"], ENT_QUOTES));
$headers = "From: $emailaddress\n" . "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1";
$recipients = array(
'cedar_lane' => 'heather@gomontessori.com',
'holly_brook' => 'heather@childersmarketing.com',
'hunter_mill' => 'ivan@gomontessori.com',
'please_select' => 'no_recipient'
);
$schoolEmail = $recipients[$_REQUEST['recipient']];
$emailsubject = array(
'cedar_lane' => 'Cedar Lane Website Submission',
'holly_brook' => 'Holly Brook Website Submission',
'hunter_mill' => 'Hunter Mill Website Submission',
'please_select' => 'no_recipient'
);
$subject = $emailsubject[$_REQUEST['recipient']];
if ($phone) {
$emailmessage = "
<p>Hello from your website!</p>
<p>$name sent you this message</p>
<p><strong>$message</strong></p>
<p>Their phone number is: $phone</p>
";
}
else {
$emailmessage = "<p>Hello from your website!</p><p>$name sent you this message</p><p><strong>$message</strong></p>";
};
$submitted = false;
if(count($_POST) > 0)
{
$submitted = true;
}
$err = true;
if ($emailmessage) {
if (validate_email($emailaddress)) {
mail($schoolEmail,$subject,$emailmessage,$headers);
$err = false;
}
};
function validate_email($email){
$bool = false;
if (eregi('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$', $email)) {
if(checkdnsrr(array_pop(explode("@",$email)),"MX")) {
$bool = true;
} else {
$bool = false;
}
} else {
$bool = false;
}
return (bool) $bool;
}
if ($submitted == true) {
if($err == true || $name == '' || $message == '' || $recipient == 'please_select') {
header("location:/clients/hollybrook/error");
exit();
}
}
?>
<html>
<head>
<title>whatever</title>
</head>
<body>
<?php if ($err == false && $submitted == true): ?>
<p>Thanks so much! If the nature of your contact requires a response or answer, we will respond to you in a timely manner.</p><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<?php else: ?>
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post" accept-charset="utf-8">
<div class='rowElem'>
<label for="name">Name</label>
<input type="text" name="name" value="" id="name" class="required" />
</div>
<div class='rowElem'>
<label for="email">Email</label>
<input type="text" name="email" value="" id="email" class="required validate-email" />
</div>
<div class='rowElem'>
<label for="phone">Phone</label>
<input type="text" name="phone" value="" id="phone" />
</div>
<div class='rowElem'>
<label for="recipient">School</label>
<select name="recipient">
<option value="please_select">Please Select A School</option>
<option value="cedar_lane">Cedar Lane</option>
<option value="holly_brook">Holly Brook</option>
<option value="hunter_mill">Hunter Mill</option>
</select>
</div>
<div class='rowElem'>
<label for="message">Comments/Questions</label>
<textarea name="message" id="message" rows="15" cols="40" class="required" ></textarea>
</div>
<div class='rowElem'><label>Looking forward to hearing from you!</label> <input name="commit" id="submit" type="submit" value="Send" /></div>
</form>
<?php endif ?>
</body>
</html>
I like the idea of keeping the form and processing on a single page, but I would be open to changing this if necessary. Thanks so much!