Question : CF - Form checking for errors with form action

I have a form that checks for errors before submitting.  The problem is when I add the needed <form  action="includes/postAAEmail.cfm" the error checking no longer happens but if that action is removed then it checks for errors.  Any help is appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
<cfparam name="FORM.Email" default="Your Email">
<cfset strError = "">

<cfif isDefined("FORM.AAsubscribe")>
  <cftry>
    <cfset form.email = trim(form.email)>
    <cfif len(form.email) eq 0 or form.email is "Your Email">
      <cfthrow message="Please enter your email address">
    <cfelseif listLen(form.email,"@") neq 2 or listLen(listLast(form.email,"@"),".") eq 0>
      <cfthrow message="Invalid Email address">
    </cfif>

  <CFQUERY NAME="GetEmail" dataSource = "#application.datasource#">
      SELECT Email
        FROM SubscriberTestEmailGroup
      WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
    </CFQUERY>
    <cfif GetEmail.recordcount gte 1>
      <cfthrow message="That email is already registered to receive the newsletter">
    </cfif> 
    
 <cfquery dataSource = "#application.datasource#">
     insert into SubscriberTestEmailGroup  (email)
     values ('#form.email#')
    </cfquery> 

  <cfcatch>
    <cfset strError = cfcatch.message>
  </cfcatch>
  </cftry>
</cfif>

<cfif len(strError)>
  <div style="position:relative;left:650px; top:8px;padding:10px; background-color:##FFFFFF;width:250px;color:maroon;text-align:right;">
 <cfoutput>#strError#</cfoutput>
 </div>
</cfif>

 <form  action="includes/sendEmail.cfm" method="post" id="AAform">
                <span>Subscribe: </span>
            <input name="Email" type="text" size="12" value="Your Email"  onFocus="this.value=''" align="middle" />
            <input name="AAsubscribe" type="submit"  onclick='$("##myButton").click();setTimeout(tb_remove,7000);' value="GO" align="middle" />
            <input id="myButton" type="hidden" alt="##TB_inline?height=300&width=400&inlineId=myOnPageContent" title="Advisor Alert Newsletter" class="thickbox"/>
</form>
                <div id="myOnPageContent" style='display:none;'>
                        <p>Thank you</p>
                </div>
            </div>
		</div>

Answer : CF - Form checking for errors with form action

> how does that code get triggered

when Coldfusion sees a CFINCLUDE, it stops processing the current file and processes the included file before proceeding.



You can take this...

<cfif isDefined("FORM.AAsubscribe")>
  <cftry>
    <cfset form.email = trim(form.email)>
    <cfif len(form.email) eq 0 or form.email is "Your Email">
      <cfthrow message="Please enter your email address">
    <cfelseif listLen(form.email,"@") neq 2 or listLen(listLast(form.email,"@"),".") eq 0>
      <cfthrow message="Invalid Email address">
    </cfif>

  <CFQUERY NAME="GetEmail" dataSource = "#application.datasource#">
      SELECT Email
        FROM SubscriberTestEmailGroup
      WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
    </CFQUERY>
    <cfif GetEmail.recordcount gte 1>
      <cfthrow message="That email is already registered to receive the newsletter">
    </cfif>
   
 <cfquery dataSource = "#application.datasource#">
     insert into SubscriberTestEmailGroup  (email)
     values ('#form.email#')
    </cfquery>

  <cfcatch>
    <cfset strError = cfcatch.message>
  </cfcatch>
  </cftry>
</cfif>



and turn it into this....


<cfif isDefined("FORM.AAsubscribe")>
  <cftry>

      <cfinclude template="SendEmail.cfm">

  <cfcatch>
    <cfset strError = cfcatch.message>
  </cfcatch>
  </cftry>
</cfif>
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
This would be the contents of SendEmail.cfm


    <cfset form.email = trim(form.email)>
    <cfif len(form.email) eq 0 or form.email is "Your Email">
      <cfthrow message="Please enter your email address">
    <cfelseif listLen(form.email,"@") neq 2 or listLen(listLast(form.email,"@"),".") eq 0>
      <cfthrow message="Invalid Email address">
    </cfif>

  <CFQUERY NAME="GetEmail" dataSource = "#application.datasource#">
      SELECT Email
        FROM SubscriberTestEmailGroup
      WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
    </CFQUERY>
    <cfif GetEmail.recordcount gte 1>
      <cfthrow message="That email is already registered to receive the newsletter">
    </cfif> 
    
    <cfquery dataSource = "#application.datasource#">
     insert into SubscriberTestEmailGroup  (email)
     values ('#form.email#')
    </cfquery> 
Random Solutions  
 
programming4us programming4us