Esküvő

PHP Contact Form Script with Captcha

Today I create a contact form with capthca verification using PHP and CSS.

PHP

<?php
session_start();
$php_self = $_SERVER['PHP_SELF'];
// on submit
if( isset($_POST[name]) && isset($_POST[email]) && isset($_POST[message]) && isset($_POST[captcha]) ){
	$name = $_POST[name];
	$email = $_POST[email];
	$message = $_POST[message];
	$captcha = $_POST[captcha];
	
	$error = 0;
	// name
	if( $name == "" ){ $error ++; $error_name = "class='error'"; }
	// email
	if( $email == "" ){ $error ++; $error_email = "class='error'"; }
	// message
	if( $message == "" ){ $error ++; $error_message = "class='error'"; }
	// captcha
	if( $captcha == "" || $captcha != $_SESSION[captcha]){ $error ++; $error_captcha = "class='error'"; }

	// no error, send email
	if( $error == 0){				
						
		// your email address
		$address = "your-email-address@domain.com";
		
		// email subject
		$subject = "New message";
		// email content
		$content = "<strong>Name:</strong><br/>$name<br/><strong>Email address:</strong><br/><strong>Message:</strong><br/>$email<br/>$message";
		// html email
		$email_content = "<!doctype html><head><meta charset='utf-8'><title>".$subject."</title>";
		$email_content .= "</head><body>";
		$email_content .= $content;
		$email_content .= "</body></html>";						
				
		// headers for html email
		$headers  = 'MIME-Version: 1.0' . "/r/n";
		$headers .= 'Content-type: text/html; charset=utf-8' . "/r/n";
		$headers .= "From: 1stWebMagazine.com <web@1stwebmagazine.com>" . "/r/n";
		// send email
		mail($address, $subject, $email_content, $headers);			
		
		// reset variables
		$name = ""; $email = ""; $message = "";
		$mail_sent = 1;																
	}
}

// captcha
$num = rand(1, 20);
$num2 = rand(1, 9);		
$verif = $num . "+" . $num2;
$_SESSION[captcha] = $num + $num2;		

if( $mail_sent == 1 ){
	echo "<h1>Thank you for your message! We'll be in touch soon.</h1>";
} else {
	echo "
	<form action='".$php_self."' method='post'>
	<p><strong>Name:</strong> <span class='required'>*</span></p>
	<input type='text' ".$error_name." name='name' value='".$name."'>
	<p><strong>Email address:</strong> <span class='required'>*</span></p>
	<input type='email' ".$error_email." name='email' value='".$email."'>
	<p><strong>Message:</strong> <span class='required'>*</span></p>
	<textarea ".$error_message." name='message'>".$message."</textarea>
	<p><strong>How much is ".$verif."?</strong> <span class='required'>*</span></p>            
	<input type='text' ".$error_captcha." name='captcha' value=''><br/>
	<button>Send</button>
	</form>";
}
?>  

CSS

input{
	height:25px;
	width:98%;
	color:#000;
	background:#FFF;
	border:1px solid #CCC;
	padding-left:1%;
	padding-right:1%;	
}
textarea{
	height:150px;
	width:98%;
	color:#000;
	background:#FFF;
	border:1px solid #CCC;
	padding:1%;
}
input.error, textarea.error, select.error{
	border:1px solid #F00;
}
button{
	height:25px;
	width:25%;
	color:#FFF;
	font-weight:bold;
	background:#36739e;
	border:1px solid #CCC;
	margin-top:10px;
}
button:hover{
	background:#86c300;
	border:1px solid #CCC;
}
.required{
	color:#F00;
}

Demo »


Esküvő


Related articles

Redirect www to non www with .htaccess RewriteRule

Redirect www to non www with .htaccess RewriteRule

05/12/2015

Redirect your websites visitors from www.domain.com to domain.com without www.

Email Validation

Email Validation

08/02/2013

Learn how to validate email on client side with HTML5, Javascript and on server side with PHP.

Generate Thumbnail on the Fly with PHP

Generate Thumbnail on the Fly with PHP

04/02/2013

In a previous artice I already showed you how to generate square thumbnails with PHP. Now I show you how to keep the original ratio of the image and how to control the alignment of the square image.


4 comments

Ben2015-11-05 17:05

Hi I have set this up but when I get the message the form sends it looks like this any ideas pleaseNew message form SW MediaName:

dumb2013-09-23 09:18

thanks for the explanation.

charm2013-04-15 11:29

it does not work

pravvy2013-04-15 11:16

How to use it please explain in detail.

Leave a comment

We welcome any comment from you! Please keep in mind that comments are moderated and rel='nofollow' is in use. So, please do not use a domain as your name or a spammy keyword, or your comment will be deleted.

Name: *

E-mail: *

Message: *

Click on the apple *


Find us on Facebook

Tags

Esküvő

Back to Top