Forum Moderators: coopster
<?php
/* Set e-mail recipient */
$myemail = "migzwebhost@gmail.com";
$subject = "Contact Form Reply";
/* Check all form inputs using check_input function */
$fullname = check_input($_POST['fullname'], "Your full name!");
$email = check_input($_POST['email'], "Your Email Address!");
$message = check_input($_POST['message'], "Your Message!");
/* If email is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Your Email Address!");
}
/* Let's prepare the message for the e-mail */
$message = "Hello Migz!
Your contact form has been submitted by:
Name: $fullname
E-mail: $email
Message: $message
End of message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect Visitor to the thank you page */
header('Location: /thanks.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<head>
:
:
</head>
<body>
:
<div class="error_message">
<h3><font style="color:#D62026;">Please correct the following error:</font></h3>
<ul>
<li><?php echo $myError; ?></li>
</ul>
</div>
:
:
</body>
</html>
<?php
exit();
}
?> [edited by: eelixduppy at 2:40 pm (utc) on Feb 3, 2012]
[edit reason] no personal URLs, please [/edit]
<?php
/* Set e-mail recipient */
$myemail = 'migzwebhost@example.com'; // SEE TOS
$subject = 'Contact Form Reply';
$this_script = 'myscript.php';
$ty_url = '/thanks.html';
//
// Let's put the error responses and fields in an array.
// We'll use it twice.
//
$responses = array(
'fullname' => 'Your full name!',
'email' => 'Your Email Address!',
'message' => 'Your Message'
);
//
// Now check it all at once
$errors = check_input($responses);
if ($errors) { echo output_form($errors,$this_script); }
else {
$errors = process_form($myemail, $subject, $responses);
// These will be DIFFERENT errors
if ($errors) { echo output_form($errors,$this_script); }
else { process_redirect($ty_url); }
}
//
///// END of entire program logic - functions follow ////
//
// Check input function. Requires an associative array of fields and messages.
// A better way would be also to pass an association of field type, e.g.,
// where I have 'email' below, one of the passed parameters defines it as email.
//
function check_input($fields) {
// set this to NULL at the outset. If no errors are found, that's what it
// will return, otherwise it returns a string containing the errors
$errors = null;
foreach ($fields as $key => $value) {
// Apply changes directly to post values so
// you can use them cleansed later
$_POST[$key] = trim($_POST[$key]);
$_POST[$key] = stripslashes($_POST[$key]);
$_POST[$key] = htmlspecialchars($_POST[$key]);
if (empty($_POST[$key])) {
$errors .= "<li>$value</li>\n";
}
}
// Only check email if no errors yet
if (! $errors and !preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $_POST['email'])) {
$errors .= "<li>Please enter a valid email address.</li>\n";
}
return $errors;
}
//
// A function to output the form if there are errors.
//
function output_form ($errors,$script) {
// just in case. Error trapping is your friend
if (! $errors) { echo "<p>WHOOPS! How did we get here?</p>"; exit; }
$form = '
<html>
<head>
<-- blah -->
</head>
<body>
<div class="error_message">
<p style="color:#D62026;">Please correct the following errors:</p><ul>' . $errors . '</ul></div>
<!-- OUTPUT YOUR FORM AGAIN HERE -->
<form method="post" action="' . $script . '">
<p><label for="email">Email:</label><span class="required">*</span>
<input type="text" name="email" id="email" value="' . $_POST['email'] . '"></p>
<p><label for="fullname">Full Name:</label><span class="required">*</span>
<input type="text" name="fullname" id="fullname" value="' . $_POST['fullname'] . '"></p>
<p><label for="message">Message</label><span class="required">*</span>
<textarea name="message" id="message">' . $_POST['message'] . '</textarea></p>
<p><input type="submit" name="submitButton" value="Send Message"></p>
</form>
';
return $form;
}
//
// Now process the form. It will also compile errors if found, and
// if so, the above logic will again use output_form.
//
function process_form($email, $subj, $fields) {
//
$errors = null;
$message = "Hello Migz!
Your contact form has been submitted by:
";
/* Let's prepare the message for the e-mail */
foreach ($fields as $key => $value) {
$message .= ucfirst($key) . ": " . $_POST[$key] . "\n";
}
$message .= "\n\n\nEnd of message\n";
// Our friend again, error trapping. :-)
if ! (mail($myemail, $subject, $message)) {
$errors .= "<li>It appears the mail function failed.</li>\n";
}
return $errors;
}
//
// After processing the form, you have two options: output the
// form again if there are mail errors, or redirect.
//
function process_redirect ($url) {
header("Location:$url"); // automatically exits
}