Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mail/Contact form validation to stop comment spam

Status
Not open for further replies.

besnette1

Technical User
Jun 19, 2001
9
Hello,
I am learning CF, so it this is a lame question, I apologize.
I am trying to make my simple contact form less easy for comment spammers. A friend suggested to put a math problem in there that has to be correct. I have been trying, and I think/hope I am close, but it isn't re-directing to my error page when the wrong answer is entered. If anyone can see what I might be doing wrong with my code below, I would be most grateful.

I hope everyone is having a wonderful day!

Basically the code is:

<form action="cfmmailto10.cfm" method="post" name="form1">
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
<textarea name="comments" cols="35" rows="4">Type comments here</textarea>
</font> <font size="2" face="Verdana, Arial"> <br>
<input name="name" type="text" value="your name" size="35">
</font><br>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
<input name="emailaddress" type="text" value="your e-mail (required)" size="35">
</font><br>
<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
What is 2 + 5? I want to make sure you are a human:
<cfset answer= 7>
<cfif answer EQ 7>
<input name="answer" type="text" value="?" size="3">
</font></p>
<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">

<input name="Submit" type="submit" value="Send it">
</font> </p>
<cfelse>
<cflocation url="(I'll put my site error url here)">
</cfif>
</form>
 
What happens when you hit submit? What destination does the user go to?

Your form action loads the page to 'cfmmailto10.cfm' what is coded on cfmmailto10.cfm? Are you submitting the page to itself or to another (processing) page?

If the page is submitted to itself the code should be something like (NOT TESTED):
Code:
<cfif isdefined("FORM.submit")>
	<cfset answer = 7>
	<cfif answer EQ 7>
		<cflocation url="DIRECT USER TO PLACE IF ANSWER IS CORRECT">
	<cfelse>
		<cflocation url="DIRECT USER TO PLACE IF ANSWER IS **NOT** CORRECT">
	</cfif>
</cfif>

<form action="cfmmailto10.cfm" method="post" name="form1">
	<p>
		<font size="2" face="Verdana, Arial, Helvetica, sans-serif"><textarea name="comments" cols="35" rows="4">Type comments here</textarea>
		</font> <font size="2" face="Verdana, Arial"> <br>
		<input name="name" type="text" value="your name" size="35">
		</font>
		<br>
		<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
		<input name="emailaddress" type="text" value="your e-mail (required)" size="35">
		</font><br>
		<font size="2" face="Verdana, Arial, Helvetica, sans-serif">
		What is 2 + 5? I want to make sure you are a human:	<input name="answer" type="text" value="?" size="3">
		</font>
	</p>
	<p><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
		<input name="Submit" type="submit" value="Send it">
	</font></p>
</form>

If the page is submitted to a processing page (ie another page that processes the code), it's same as above, except the <cfif> statements are on a 2nd file.



____________________________________
Just Imagine.
 
Hello,
Thank you for taking the time to look at it. Really appreciated. The cfmailto10.cfm processing file code (before making any changes) was as follows (you indicate that the cfif statements should be on this page?):

<cfmail
from="laptop@site.com"
to="laptop@site.com"

subject="site.com contact"
server="mail.site.com">

#form.comments#
#form.name#
#form.emailaddress#
#REMOTE_HOST#

</cfmail>
<cflocation url="MY SITE URL>
 
PS - the destination right now is just a basic "Thank you" html page.
 
besnette1, yeah the processing page should looks something like this (keep in mind that you can do so much with this page, like have additional error-trapping code, validate other ways and redirect in numerous different locations)

Code:
<cfif isdefined("FORM.submit") and FORM.answer NEQ "?">

  <!--- VALIDATE THAT FORM.ANSWER VALUE IS CORRECT, IF INCORRECT REDIRECT USER --->
  <cfparam name = "FORM.answer" default = "7">
  <cfif FORM.answer EQ 7>
    <!--- DO NOTHING --->
  <cfelse>
    <cflocation url="backtoform.cfm?status=1">
  </cfif>

  <!--- SEND E-MAIL IF VALIDATION WORKS --->
  <cfmail from="laptop@site.com" to="laptop@site.com" subject="site.com contact" server="mail.site.com">
    #form.comments#
    #form.name#
    #form.emailaddress#
    #REMOTE_HOST#
  </cfmail>

  <!--- IF ALL IS GOOD, REDIRECT USER --->
  <cflocation url="MY SITE URL>
<cfelse>
  <!--- SUBMIT BUTTON NOT CLICKED OR FORM.ANSWER IS NULL (IN YOU CASE PASSED THE '?' VALUE --->
  <cflocation url="backtoform.cfm?status=1">
</cfif>

This is a bare skull-n-bones template code you can start with. You can also check if other fields that are required are not null or passed with incorrect data.



____________________________________
Just Imagine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top