
Nov 21st, 2007, 14:02
|
|
Reputable Member
|
|
Join Date: Oct 2007
Location: UK
Posts: 267
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Contact Form Validation
Hey everyone, can u have a look at my code.....I need to add some sort of validation that will display a little message underneath my 'submit' button saying "Email sent" if it was posted or "error" if it wasnt.
I dont know how to do this
- Code: Select all
<?
error_reporting(E_ALL);
ini_set('display_errors', '1');
if(isset($_POST['submit'])) {
$to = "danielm@design365.co.uk";
$subject = "Genetics Feedback";
$name_field = $_POST['name'];
$telephone_field = $_POST['telephone'];
$email_field = $_POST['email'];
$comments_field = $_POST['comments'];
$body = "From: $name_field\n E-Mail: $telephone_field\n Telephone: $email_field\n Comments:\n $comments";
mail($to, $subject, $body);
} else {
echo "Error! Please try again";
}
function antispam()
{
// First, make sure the form was posted from a browser.
// For basic web-forms, we don't care about anything
// other than requests from a browser:
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
die("Forbidden - You are not authorized to view this page");
exit;
}
// Make sure the form was indeed POST'ed:
// (requires your html form to use: action="post")
if (!$_SERVER['REQUEST_METHOD'] == "POST") {
die("Forbidden - You are not authorized to view this page");
exit;
}
// Make sure the form was posted from an approved host name.
if (!strstr($_SERVER['HTTP_REFERER'], "cashincar")) {
//logBadRequest();
header("HTTP/1.0 403 Forbidden");
exit;
}
// Attempt to defend against header injections:
$badStrings = array("Content-Type:",
"MIME-Version:",
"Content-Transfer-Encoding:",
"bcc:",
"cc:",
"http://www.genetics.co.uk/contact.php");
// Loop through each POST'ed value and test if it contains
// one of the $badStrings:
foreach($_POST as $k => $v){
foreach($badStrings as $v2){
if (strpos($v, $v2) !== false) {
// logBadRequest();
header("HTTP/1.0 403 Forbidden");
exit;
}
}
}
}
/*// do stuff to the post
if ($_POST) {
antispam();
while (list($key, $value) = each($_POST)) {
$$key = $value;
$message .=$key.": ".$value."\n";
}
//mail form
$message = "There has been a message/request from ".$_SERVER['HTTP_HOST']."\n\n".$message;
$message .="-----------------------\n\n";
$message = stripslashes($message);
mail("danielm@design365.co.uk", "Genetics Feedback", $message, "From: danielm@design365.co.uk");
}*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>geNetics Web - Next Generation Web Development</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="gen.css">
</head>
<body>
<div id="wrapper">
<div id="about_us">
<ul>
<li><a href="index.html"> Home</a></li>
<li><a href="our_team.html">Our team</a></li>
<li><a href="about_us.html">About us</a></li>
<li><a href="our_work.html">Our work</a></li>
<li><a href="contact.php"class="yourhere">Contact</a></li>
</ul>
<p>Please don't hesitate to contact us, we will be more than happy to help. Simply fill out the form below:</p>
<div class="form"><form name="contact" action="contact.php" method="POST">
Your Name:<br>
<input class="form_style" name="name" type="text" size="20" maxlength="30"/><br><br>
Telephone:<br>
<input class="form_style" name="telephone" type="text" size="20" maxlength="20" /><br><br>
Email:<br>
<input class="form_style" name="email" type="text" size="20" maxlength="50" /><br><br>
Comments and Questions:<br>
<textarea class="form_style" name="comments" cols="20" rows="5"></textarea><br>
<input class="submitbtn" name="submit" type="submit" value="Submit"/>
</form>
<?echo "Data has been submitted to $to!";?>
</div>
</div>
<div id="footer">Next generation web development</div>
</body>
</html>
|