I saw this tip on another web site to prevent spam on contact forms. In the contact form you add a hidden field
Code:
<input name="to_address" type="hidden" value="" />
And then in the PHP file you add
Code:
if ( $_POST['to_address'] ) { echo 'Tastes Like spam!'; } else {...send email...}
But my PHP file is a little different as also activates some JavaScript on the webpage depending on whether the form is filled out correctly or not (If successful shows a little ‘Your email has been sent...’ with animation. If the fields aren’t filled out correctly it shows what’s missing).
Someone else made the contact form for me as I don’t know any PHP (or ajax), but I’ve tried to adapt the code above & add it to my PHP form. I’ve tested it & it seems to work OK, but thought I’d just check that it shouldn’t potentially cause any problems, or if it needs tweaking/formatting a little.
Here’s the last part of my original PHP file
Code:
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = true; mail($EmailTo, $Subject, $Body, "From: <$Email>");
}
// call success or error js functions
if ($success){
print "<div id=\"ajaxcode\">doThanks()</div>";
}
else{
print "<div id=\"ajaxcode\">doError()</div>";
}
?>
And here it is after I added the (hopefully) anti spam code (highlighted in green)
Code:
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = true; mail($EmailTo, $Subject, $Body, "From: <$Email>");
}
// call success or error js functions
if ( $_POST['to_address'] ) { echo 'Tastes Like spam!'; } else
if ($success){
print "<div id=\"ajaxcode\">doThanks()</div>";
}
else{
print "<div id=\"ajaxcode\">doError()</div>";
}
?>
Does the code I added look OK, or does it need changing in any way in order or it to function properly? 
Last edited by FOOOD; 03-17-2009 at 05:03 PM..
|