PHP Captcha Email Submission
Below is a free coding example of how to make a PHP Captcha Email Submission.
.header {height:250px;}
Code Example: (Step: 1)
<head>
</head>
<body>
<div class="container">
<iframe src="form.php" frameborder="0" scrolling="no" height="330" width="460" style="*margin-top:20px;"></iframe>
<div>
</body>
</html>
Explanation:

What is going on here is that I want the actual PHP file to run on a separate page so that when it runs the "thank you for submitting," the entire page will not refresh on your index.html page. The iFrame works: "<iframe src="form.php" frameborder="0" scrolling="no" height="330" width="460" style="*margin-top:20px;"></iframe>" by sourcing the from.php then it turns off the frameborder and scrolling. After that I have set a height of 330 pixels and width of 460 pixels. The last thing is that I set a holly hack to move the margin-top by 20 pixels if you are using IE.
Code Example: (Step: 2)
foreach($_POST as $key => $val){$$key = $val;}
function getRandNumber(){
//rand(starting number, ending number);
$val = rand(1,10);
return $val;
}
function sendEmail(){
foreach($_POST as $key => $val){$$key = $val;}
//your email address
$recipient = "youremail@email.com";
//your contact form, any desired address
$sender = "contact@combsconsulting.com";
//subject of email
$subject = "New Contact";
//construct body content
$content = "Name: \n".$name."\n\n Email:\n".$email."\n\n Message:\n".$message;
//headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text; charset=iso-8859-1" . "\r\n";
$headers .= "To: ".$cecipients . "\r\n";
$headers .= "From: ".$sender . "\r\n";
$mailcontent = str_replace("\n", "\r\n", $content);
mail($recipient,$subject,$mailcontent,$headers);
}
if(!empty($submit)){
//validate required fields
if(empty($name)){$nameStyle = "visibility:visible;";}
else{$nameStyle = "visibility:hidden;";}
if(empty($email)){$emailStyle = "visibility:visible;";}
else{$emailStyle = "visibility:hidden;";}
if(empty($message)){$mssgStyle = "visibility:visible;";}
else{$mssgStyle = "visibility:hidden;";}
if(empty($answer)){$anserStyle = "visibility:visible;";$amHuman = "Answer Required!";}
else{$answerStyle = "visibility:hidden;";}
//am Human validation
if(!empty($name) && !empty($email) && !empty($message) && !empty($answer)){
$val1 = $val1;
$val2 = $val2;
$answer = $answer;
$finalVal = ($val1 + $val2);
if($finalVal == $answer){
echo "<div style='height:330px;width:470px;margin:-5px 0 0 -5px;*margin:-15px 0 0 -10px;
background:#ccdfed;font-size:21px;color:#26353D;font-weight:bold;'><p style='margin-top:20px;'>Thank For
Contacting Combs Consulting, LLC.</p></div>";
sendEmail();
exit;
}
else{
$amHumanStyle = "visibility:visible;";
$amHuman = "Invalid answer!";
}
}
else{
$val1 = getRandNumber();
$val2 = getRandNumber();
}
}
//set styles to default configuration
else{
$nameStyle = "visibility:hidden;";
$emailStyle = "visibility:hidden;";
$mssgStyle = "visibility:hidden;";
$anserStyle = "visibility:hidden;";
$val1 = getRandNumber();
$val2 = getRandNumber();
}
?>
<html>
<head>
<style type='text/css'>
<!--
body{*background:#ccdfed;}
.fieldset{border:none;width:330px;height:303px;}
.fieldset p{margin-bottom:-11px;*margin-bottom:-10px;}
input, textarea{font-size:110%;padding:4px;border:none;font-family:Helvetica, Arial, Tahoma, sans-serif;background:#6f8b9b;clear:both;}
label, .label{font-weight:bold;color:#394f5f;}
textarea{height:100px;font-family:Helvetica, Arial, Tahoma, sans-serif;}
.txt{width:296px;color:#517990;border:none;background:#abcadb;}
.inputHighlighted{width:296px;background-color:#6f8b9b;}
.submit{margin:-2px 0 0 -4px;*margin-left:0;background:url('http://www.combsconsulting.com/images/toggler/sendBtn3.gif') no-repeat 0 0;width:90px;height:25px;border:none;cursor:pointer;color:#fff;font-size:14px;font-weight:bold;
*padding-top:2px;}
@media screen and (-webkit-min-device-pixel-ratio:0){.submit{padding-top:3px;}}
.submit span{margin-left:-17px;line-height:20px;}
.answer{margin-top:60px;font-size:21px;color:#26353D;font-weight:bold;}
.error{color:#f00;padding-left:10px;}
-->
</style>
</head>
<body>
<form action="<?=$PHP_SELF;?>" name="myForm" method="post">
<fieldset class="fieldset">
<p style="margin-top:2px;*margin-top:-10px;position:relative;">
<label>Name:</label>
<span class="error" style="<?=$nameStyle;?>">Name Required!</span>
<input type="text" class='txt' name="name" value="<?=$_POST[name];?>" />
</p>
<p>
<label>Email:</label>
<span class="error" style="<?=$emailStyle;?>">Email Required!</span>
<input type="text" name="email" class='txt' value="<?=$_POST[email];?>" />
<p>
<p>
<label>Message:</label>
<span class="error" style="<?=$mssgStyle;?>">Message Required!</span>
<textarea name="message" cols="40" rows="10" class='txt'><?=$_POST[message];?></textarea>
</p>
<p>
<span class="label" id="val1"><?=$val1;?></span> +
<span class="label" id="val2"><?=$val2;?></span> =
<input type="hidden" name="val1" class='txt' value="<?=$val1;?>" />
<input type="hidden" name="val2" class='txt' value="<?=$val2;?>" />
<span class="error" style="<?=$anserStyle;?>"><?=$amHuman;?></span>
<input type="text" name="answer" class='txt' value="<?=$answer;?>" />
<p>
<p>
<button type="submit" name="submit" class='submit' value="Send"><span>Send</span></button>
</p>
</fieldset>
</form>
</body>
</html>
Explanation:

What is going on here is that there are so many bots out there we have to build a Captcha that will prevent them from submitting. This Captcha works by making the user add two numbers before you are allowed to submit the form.
(There are other Captchas out there that use pictures, but then you have to make all the pictures and that takes a lot of time. )
PHP Header Section
I’m not going step through every part of the code, I’m just going to give you a "fly-by."
The first part of the PHP asks the server to randomely generate a number between 1 and 10: "$val = rand(1,10);"
The next part tells the server where to send the email: "$recipient = "youremail@email.com";"
The next part tells the server what to tell the email where this email is from: "$sender = "contact@combsconsulting.com";"
(Note: I did this so that you can asign an email that you can tell your email reader this is not spam and it automatically lets it through)
The next part tells the server what to label the subject header of the email it is sending: "$subject = "New Contact";"
The next part spits out the format of the email of Name line break Email line break and Email and line break: "$content = "Name: \n".$name."\n\n Email:\n".$email."\n\n Message:\n".$message;"
In the final validatin section where you see "echo" is where the "thank you for submitting" is drawn. If you want to have have styles and formating you need to attach them directly.
(Note: if you dont need the formating just take out the divs and have it say, "Thank You For Contacting Me".):
"echo "<div style='height:330px;width:470px;margin:-5px 0 0 -5px;*margin:-15px 0 0 -10px;background:#ccdfed;font-size:21px;color:#26353D;font-weight:bold;'><p style='margin-top:20px;'>Thank For Contacting Combs Consulting, LLC.</p></div>";"
(Also Note: That in the style for the dive being drawn I made a background color, controled the height, width, and margins because when the iFrame refreshes; IE puts the background color back to white. This is a major problem is you have a background color other than white.)
Style Header Section

I’m not going step through every part of the code, I’m just going to give you a "fly-by" of what is important
There is a problem with IE getting the form to submit using a image so I created a button image, with no text in that image, and then pushed the text in the <button> tag around to get it to work. The
".submit" class has a *padding-top:2px; for IE and Safari need a hack for the padding as well "@media screen and (-webkit-min-device-pixel-ratio:0){.submit{padding-top:3px;}}."
The problem is to center the text in the <span> tag in the button: ".submit span{margin-left:-17px;}."
Body Section
I’m not going to explain much here, just look at the code and play with the styles and you will figure it out. If you have any questions you can go to my site and email me.
