- Joined
- Dec 20, 2010
- Messages
- 418
- Reaction score
- 605
This is a very simple contact form, with also a minor anti-spam check. There are a lot of unnessecary variables being made (if anyone wants to bother - feel free to clean it up).
PHP:
The form itself:
And to show the error/success code, use this:
Enjoy.
PHP:
PHP:
<?php
session_start();
$hour = date("H");
$minute = date("i");
$seconds = date("s");
$day = date("d");
$month = date("m");
$year = date("Y");
$extratime = 30;
$current = mktime($hour,$minute,$seconds,$month,$day,$year);
$expire = mktime($hour,$minute,$seconds+$extratime,$month,$day,$year);
$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'YOUR@EMAIL.com';
$subject = 'Message from '.$firstname.' '.$surname.' with IP '.$_SERVER['REMOTE_ADDR'].'';
$headers = "MIME-version: 1.0\r\n";
$headers .= "content-type: text/html;charset=utf-8\r\n";
if(isset($_POST['send']))
{
$check_status = '';
$errors = array();
if (isset($_SESSION['check']) && $_SESSION['check'] > $current)
{
$errors[] = 'Please wait '.$extratime.' seconds before sending another message..';
}
if(empty($firstname))
{
$errors[] = 'The field "First name" is empty!';
}
if(empty($surname))
{
$errors[] = 'The field "Surname" is empty!';
}
if(empty($email))
{
$errors[] = 'The field "E-mail address" is empty!';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !empty($email))
{
$errors[] = 'The given e-mail address is invalid. Please fill in a good one.';
}
if(empty($message))
{
$errors[] = 'The field "Message" is empty!';
}
if(count($errors) == 0)
{
$headers .= 'From: ' . $firstname . ' ' . $surname . '<' . $email . '>';
if(mail($to, $subject, nl2br($message), $headers))
{
$check_status = 'Your message has been succesfully send!!';
$_SESSION['check'] = $expire;
}
else
{
$errors[] = 'A system error occured.';
}
}
}
foreach($errors as $key => $keyvalue){
$check_status .= ''.$keyvalue.'<br>';
}
?>
The form itself:
Code:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First name<br />
<input type="text" name="firstname" /><br /><br />
Surname<br />
<input type="text" name="surname" /><br /><br />
E-mail address<br />
<input type="text" name="email" /><br /><br />
Your message<br />
<textarea cols="50" rows="12" name="message"></textarea><br /><br />
<input type="reset" value="Empty fields" /> <input type="submit" name="send" value="Send message" />
</form>
And to show the error/success code, use this:
PHP:
<?php
if (isset($check_status) && $check_status != ''){
echo $check_status;
}
?>
Enjoy.