With a little side of applesauce...

Tuesday, January 30, 2007

PHP email forms

Here is an excerpt from an email showing how to create PHP email forms:



<pre class="tightenable top bottom">The examples that would be most interesting to you, would be the html form:

<form action="mail.php" method="post">
Your Name: <input type="text" name="name">

E-mail: <input type="text" name = "email">


Comments

<textarea name="comments"></textarea>


<input type="submit" value="Submit">
</form>


And, the php script "mail.php" in the same directory:


// This function helps to keep spammers from using your forms
function checkOK($field)
{
if (eregi("\r",$field) || eregi("\n",$field)){
die("Invalid Input!");
}
}

// set the posted variables to local variables
// This makes it easier for accessing the form data later in the script.
// NOTE: Change the value of foo in $_POST['foo'] to the name of your form
// on the previous page. Also, you can add as many of these initializations
// as are forms on the previous page.
$name=$_POST['name'];
checkOK($name);

$email=$_POST['email'];
checkOK($email);

$comments=$_POST['comments'];
checkOK($comments);

// We are setting the recipients email address here
$to="myemail@myweb.com";

// We are creating the message content here. Notice that the variables that we set above are used
// throughout the message to insert the form information.
$message="$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email";

// Now, we try to send the mail. If it is successful, we return a "Thanks for your comments",
// If it is not successful, we return "There was a problem sending the mail...."
if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>

The code is from:

http://www.freewebmasterhelp.com/tutorials/php/6

and I just added comments.

No comments: