Prev | Current Page 416 | Next

Craig Grannell

"The Essential Guide to CSS and HTML Web Design"

php. Amend the opening form tag like this:

3. At the top of the page, insert the following PHP code block above the DOCTYPE.
Although I??™ve warned you elsewhere in the book never to place any content above
the DOCTYPE, it??™s perfectly safe to do so in this case, because the PHP code doesn??™t
produce any HTML output.
if (array_key_exists('SUBMIT', $_POST)) {
//mail processing script
$to = 'me@example.com'; // use your own email address
$subject = 'Feedback from website';
// list expected fields
$expected = array('realname', 'email', 'phone', 'message');
// set required fields
$required = array('realname', 'email', 'message');
$headers = 'From: My website';
$process = 'process_mail.inc.php';
if (file_exists($process) && is_readable($process)) {
include($process);
}
else {
$mailSent = false;
GETTING USER FEEDBACK
329
8
mail($to, 'Server problem', "$process cannot be read", $headers);
}
}
?>
4. This script begins by checking whether the PHP $_POST array has been set. This
happens only when a user clicks the form??™s Submit button, so this entire block of
code will be ignored when the page first loads. It sets the address to which the
e-mail is to be sent and the subject line. It then checks that all required fields have
been filled in, and sends the form input for processing by process_mail.inc.php.
If the mail processing file can??™t be found, the script e-mails an error message
to you.


Pages:
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428