CONTENTS:
XHTML.
Validating user input.
the mail() function.
Here's the finished .php file for the eager ones:
form_mail.php (3.14K)
Number of downloads: 21
I'd suggest reading throught the following posts....
Posted 21 February 2008 - 09:36 AM
form_mail.php (3.14K)
Posted 21 February 2008 - 09:37 AM
<!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" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>form mail</title>
<style type="text/css" media="all">
* { margin:0;padding:0; }
body { font:600 12px/22px verdana;padding:50px; }
.box, textarea, .button { border:2px solid #6699CC;font:inherit;color:inherit; }
.box { width:250px; }
textarea { width:300px;height:250px; }
.error { color:#DD1100; }
</style>
</head>
<body>
<div class="contact">
<h1>Contact Us</h1>
<p>Please fill out the form to contact us. Required fields are marked with a star [*].</p>
<form name="contact" action="form_mail.php" method="post">
<p>Name:* <input class="box" type="text" name="name" /></p>
<p>E-Mail:* <input class="box" type="text" name="email" /></p>
<p>Subject: <input class="box" type="text" name="subject" /></p>
<p>Message:*</p><textarea name="message"></textarea>
<p><input class="button" type="submit" action="submit" value="Send" />
</form>
</div>
</body>
</html>
Posted 21 February 2008 - 09:39 AM
// declare and initialize some variables
$name=$email=$subject=$message=$error_message='';
$invalid_fields=array();
$required_fields=array('name','email','message');
$validated=array();
$to_address='blub@bla.com'; // your email address goes here// validate $_POST array
if(count($_POST)>0) { // was something posted?
foreach($_POST as $key=>$value) { // loop through the $_POST array
if(in_array($key,$required_fields)&&$value=='') { // check if a required field is empty
// add that field to the $invalid_fields array
array_push($invalid_fields,$key);
// and append the error message to the $error_message variable
$error_message.='<p>Please enter a'.(preg_match('/^[aeiouy]/',$key)?'n':'').' '.$key.'.</p>';
}
// field is not in the $invalid_fields array?
if(!in_array($key,$invalid_fields)) {
// copy it to the $validated array
$validated[$key]=htmlspecialchars($value);
}
}
} else { // make everything invalid so that the form is outputted and not the thankyou message
$invalid_fields=$required_fields;
}
Posted 21 February 2008 - 09:41 AM
<div class="contact">
<h1>Contact Us</h1>
<?php
if(count($invalid_fields)<=0) { // send message / thank user
// mail will get sent here
?>
<p>Your message was successfully delivered.</p>
<?php } else { ?>
<p>Please fill out the form to contact us. Required fields are marked with a star [*].</p>
<?php echo(($error_message!=''?'<div class="error">'.$error_message.'</div>':'')); ?>
<form name="contact" action="form_mail.php" method="post">
<p>Name:* <input class="box" type="text" name="name" value="<?php echo($validated['name']); ?>" /></p>
<p>E-Mail:* <input class="box" type="text" name="email" value="<?php echo($validated['email']); ?>" /></p>
<p>Subject: <input class="box" type="text" name="subject" value="<?php echo($validated['subject']); ?>" /></p>
<p>Message:*</p><textarea name="message"><?php echo($validated['message']); ?></textarea>
<p><input class="button" type="submit" action="submit" value="Send" />
</form>
<?php } ?>
</div>Explanations:foreach($_POST as $key=>$value) { // loop through the $_POST array
if(in_array($key,$required_fields)&&$value=='') { // check if a required field is empty
// add that field to the $invalid_fields array
array_push($invalid_fields,$key);
// and append the error message to the $error_message variable
$error_message.='<p>Please enter a'.(preg_match('/^[aeiouy]/',$key)?'n':'').' '.$key.'.</p>';
} else {
switch($key) {
case 'email': // validate email address format
if(!preg_match('/^([A-Z0-9._%-]+)@([A-Z0-9.-]+)\.([A-Z]{2,6})$/i',$value);) {
array_push($invalid_fields,'email');
$error_message.='<p>Please enter a valid email.</p>';
}
break;
}
}
// field is not in the $invalid_fields array?
if(!in_array($key,$invalid_fields)) {
// copy it to the $validated array
$validated[$key]=htmlspecialchars($value);
}
}Explanations:<?php
if(count($invalid_fields)<=0) { // send message / thank user
$formatted_message='When: '.date('r').'
Who: '.$name.' ('.$email.')
With What: '.$_SERVER['HTTP_USER_AGENT'].'
Message: '.$message;
mail($to_address,$subject,$formatted_message);
?>Explanations:<?php
/****** PHP Form Mail *****/
// declare and initialize some variables
$name=$email=$subject=$message=$error_message='';
$invalid_fields=array();
$required_fields=array('name','email','message');
$validated=array();
$to_address='blub@bla.com'; // your email address goes here
// validate $_POST array
if(count($_POST)>0) { // was something posted?
foreach($_POST as $key=>$value) { // loop through the $_POST array
if(in_array($key,$required_fields)&&$value=='') { // check if a required field is empty
// add that field to the $invalid_fields array
array_push($invalid_fields,$key);
// and append the error message to the $error_message variable
$error_message.='<p>Please enter a'.(preg_match('/^[aeiouy]/',$key)?'n':'').' '.$key.'.</p>';
} else {
switch($key) {
case 'email': // validate email address format
if(!preg_match('/^([A-Z0-9._%-]+)@([A-Z0-9.-]+)\.([A-Z]{2,6})$/i',$value)) {
array_push($invalid_fields,'email');
$error_message.='<p>Please enter a valid email.</p>';
}
break;
}
}
// field is not in the $invalid_fields array?
if(!in_array($key,$invalid_fields)) {
// copy it to the $validated array
$validated[$key]=htmlspecialchars($value);
}
}
} else { // make everything invalid so that the form is outputted and not the thankyou message
$invalid_fields=$required_fields;
}
?>
<!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" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>form mail</title>
<style type="text/css" media="all">
* { margin:0;padding:0; }
body { font:600 12px/22px verdana;padding:50px; }
.box, textarea, .button { border:2px solid #6699CC;font:inherit;color:inherit; }
.box { width:250px; }
textarea { width:300px;height:250px; }
.error { color:#DD1100; }
</style>
</head>
<body>
<div class="contact">
<h1>Contact Us</h1>
<?php
if(count($invalid_fields)<=0) { // send message / thank user
$formatted_message='When: '.date('r').'
Who: '.$name.' ('.$email.')
With What: '.$_SERVER['HTTP_USER_AGENT'].'
Message: '.$message;
// send the email
mail($to_address,$subject,$formatted_message);
?>
<p>Your message was successfully delivered.</p>
<?php } else { ?>
<p>Please fill out the form to contact us. Required fields are marked with a star [*].</p>
<?php echo(($error_message!=''?'<div class="error">'.$error_message.'</div>':'')); ?>
<form name="contact" action="form_mail.php" method="post">
<p>Name:* <input class="box" type="text" name="name" value="<?php echo($validated['name']); ?>" /></p>
<p>E-Mail:* <input class="box" type="text" name="email" value="<?php echo($validated['email']); ?>" /></p>
<p>Subject: <input class="box" type="text" name="subject" value="<?php echo($validated['subject']); ?>" /></p>
<p>Message:*</p><textarea name="message"><?php echo($validated['message']); ?></textarea>
<p><input class="button" type="submit" action="submit" value="Send" />
</form>
<?php } ?>
</div>
</body>
</html>Good Luck with your project!| Trackback URL | Trackback Date | Total Hits |
|---|---|---|
| Topic | Started By | Stats | Last Post Info | |
|---|---|---|---|---|
![]() |
Directaz Llc - Small Business Web Design
|
Jasontor ![]() |
|
|
![]() |
Hi, I Got The Mail
|
caminowebmaster ![]() |
|
|
![]() |
Mobile Version
|
craig ![]() |
|
|
![]() |
The Css Tutorial On This Site
|
shammy2007 ![]() |
|
|
![]() |
|
xkatx21x ![]() |
|
|