Link to home
Start Free TrialLog in
Avatar of JohnMac328
JohnMac328Flag for United States of America

asked on

CF - Using cflocation with form submit

I tried asking a question where a form no longer shows and a message is displayed in place of the form after it is submitted but could not get an answer.  Now I am trying to do a <cflocation url to display the message by itself.  Now the problem is that the form checking does not work even when I put the cf location after all the edit checks.  Or if you can tell me how to have the form not display after it is sucessfully submitted that would work too.  Any help is appreciated.


<cfset datasource = "Hbanana">
 <cfparam name="FORM.Email" default="">
 <cfparam name="FORM.firstname" default="">
 <cfparam name="FORM.lastname" default="">
 <cfparam name="FORM.FUND" default="">
 <cfparam name="FORM.GROW" default="">

<!--- Create an empty error string --->
<cfset strError = "">

<!--- If the form is submitted --->
<cfif isDefined("FORM.Submit")>
<!--- <cfif not isdefined("FORM.Submit")> --->

<!---    <cfset strError = ""> --->


      <cfset FORM.Email = Trim(FORM.Email)>
            <cfif Len(Trim(FORM.Email)) LT 1>
            <cfset strError = strError & "Please enter your Email!<br>">
            </cfif>
       
      <cfif not len(strError)>    
         <CFQUERY NAME="GetEmail" DATASOURCE="#datasource#">
           SELECT  Email
           FROM    Table
           WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
        </CFQUERY>

        <cfif GetEmail.recordcount GT 0>
          <cfset strError = strError & "That email is already registered <br>to receive the newsletter<br>">
        <cfelse>
           <cfquery  NAME="Added" datasource="#datasource#">
            INSERT INTO Table (Email,firstname, lastname, FUND, GROW)
            VALUES ('#FORM.Email#','#FORM.firstname#','#FORM.lastname#','#FORM.FUND#','#FORM.GROW#')
            </cfquery>
           </cfif>
<cflocation url = "http://localhost/JQUERY_EMAIL_SIGNUP/SignupConfirmation.cfm" addToken = "no">            

            <cfmail>
Mail to send
         </cfmail>  
            </cfif>
     </cfif>

Open in new window

Avatar of gdemaria
gdemaria
Flag of United States of America image

I think the only problem is the placement of the CFLOCATION, it has to be in a place that will only be reached if everything is successful (no errors found).  Currently, it is not.  It is running even if there is an error.  

Move it inside the </CFIF> where the insert statement is.

Note that the CFMAIL will never be reached because the CFLOCATION will take you to another page


        <cfif GetEmail.recordcount GT 0>
          <cfset strError = strError & "That email is already registered <br>to receive the newsletter<br>">
        <cfelse>
           <cfquery  NAME="Added" datasource="#datasource#">
            INSERT INTO Table (Email,firstname, lastname, FUND, GROW)
            VALUES ('#FORM.Email#','#FORM.firstname#','#FORM.lastname#','#FORM.FUND#','#FORM.GROW#')
            </cfquery>
            <cflocation url = "http://localhost/JQUERY_EMAIL_SIGNUP/SignupConfirmation.cfm" addToken = "no">            

        </cfif>

Avatar of JohnMac328

ASKER

Yeah, I just realized that and I have to have the email go after the submit but they don't want the form to show in the iFrame after the form submits
personally, I don't like having all the nested CFIF statements, this method of using CFCATCH can handle your custom errors as well as any unintentional errors created by a bad SQL statement or something..

<cftry>

      <cfset FORM.Email = Trim(FORM.Email)>

         <!---- VALIDATE ----->
         <cfif Len(FORM.Email) eq 0>
           <cfthrow message="Please enter your Email">
         </cfif>

         <CFQUERY NAME="GetEmail" DATASOURCE="#datasource#">
           SELECT  Email
           FROM    Table
           WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
        </CFQUERY>
        <cfif GetEmail.recordcount GT 0>
          <cfthrow message="That email is already registered <br>to receive the newsletter">
        </cfif>
      

        <!---- ACTION ---->
           <cfquery  NAME="Added" datasource="#datasource#">
            INSERT INTO Table (Email,firstname, lastname, FUND, GROW)
            VALUES ('#FORM.Email#','#FORM.firstname#','#FORM.lastname#','#FORM.FUND#','#FORM.GROW#')
          </cfquery>
        
        <!--- SUCCESS, REDIRECT --->
        <cflocation url = "http://localhost/JQUERY_EMAIL_SIGNUP/SignupConfirmation.cfm" addToken = "no">            

 <cfcatch>
   <cfset error = cfcatch.message>
 </cfcatch>
</cftry>    

<cfif len(error)>
<div style="color:red">#error#</div>
</cfif>

Open in new window

> but they don't want the form to show in the iFrame after the form submits

You can use CFLOCATION to go to another page (as you're doing now), or you can simply put in a CFEXIT to stop processing and not show the form again..

So the cfexit would send the mail, not show the form and display the message?
> So the cfexit would send the mail, not show the form and display the message?

If you put it in the correct order...


Congratulations You did it !
<cfmail....>
<cfexit>  <!==== stop processing here



Using my example below...

<cftry>

      <cfset FORM.Email = Trim(FORM.Email)>

         <!---- VALIDATE ----->
         <cfif Len(FORM.Email) eq 0>
           <cfthrow message="Please enter your Email">
         </cfif>

         <CFQUERY NAME="GetEmail" DATASOURCE="#datasource#">
           SELECT  Email
           FROM    Table
           WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
        </CFQUERY>
        <cfif GetEmail.recordcount GT 0>
          <cfthrow message="That email is already registered <br>to receive the newsletter">
        </cfif>
      

        <!---- ACTION ---->
           <cfquery  NAME="Added" datasource="#datasource#">
            INSERT INTO Table (Email,firstname, lastname, FUND, GROW)
            VALUES ('#FORM.Email#','#FORM.firstname#','#FORM.lastname#','#FORM.FUND#','#FORM.GROW#')
          </cfquery>
        
          Congratulations!
          <cfmail...>All is well</cfmail>
          <cfexit>

 <cfcatch>
   <cfset error = cfcatch.message>
 </cfcatch>
</cftry>    

<cfif len(error)>
<div style="color:red">#error#</div>
</cfif>

Open in new window

It is telling me this error but I have the cfcatch the last tag

The start tag must have a matching end tag. This could be because <cfcatch> is not the last tag nested in the <cftry>. <cfcatch> must be the last tag inside a <cftry> tag.
<cfif isDefined("FORM.Submit")>


<cftry>
      <cfset FORM.Email = Trim(FORM.Email)>
         <cfif Len(FORM.Email) eq 0>
           <cfthrow message="Please enter your Email">
       
      <cfif not len(strError)>    
         <CFQUERY NAME="GetEmail" DATASOURCE="#datasource#">
           SELECT  Email
           FROM    Table
           WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
        </CFQUERY>

        <cfif GetEmail.recordcount GT 0>
           <cfthrow message="That email is already registered <br>to receive the newsletter">
        <cfelse>
           <cfquery  NAME="Added" datasource="#datasource#">
            INSERT INTO Table (Email,firstname, lastname, FUND, GROW)
            VALUES ('#FORM.Email#','#FORM.firstname#','#FORM.lastname#','#FORM.FUND#','#FORM.GROW#')
            </cfquery>

			<h5>Thank You for Signing up </h5>
		   <p>You should start receiving</p> 
           </cfif>

		<cfmail 
		
   </cfmail>  
		</cfif>
     </cfif>
  </cfif>
<cfexit>
     
 <cfcatch>
   <cfset error = cfcatch.message>
 </cfcatch>
</cftry>    

<cfif len(error)>
<div style="color:red">#error#</div>
</cfif>

Open in new window

Your CFIF tags are out of order, and most of the CFELSE tags are unnecessary (getting rid of them is why I used the CFTHROW)

I have removed the unnecessary CFELSE tags and moved your OUTER MOST CFIF tag to outside the </cftry>

The <CFMAIL tag was not complete so commented it out to avoid errors

please try the code below and seehow it goes

<cfset error = "">

<cfif isDefined("FORM.Submit")>
   <cftry>
         <cfset FORM.Email = Trim(FORM.Email)>
         
         <cfif Len(FORM.Email) eq 0>
             <cfthrow message="Please enter your Email">
         </cfif>       
         
         <CFQUERY NAME="GetEmail" DATASOURCE="#datasource#">
           SELECT  Email
           FROM    Table
           WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
        </CFQUERY>

        <cfif GetEmail.recordcount GT 0>
          <cfthrow message="That email is already registered <br>to receive the newsletter">
        </cfif>
        

           <cfquery  NAME="Added" datasource="#datasource#">
            INSERT INTO Table (Email,firstname, lastname, FUND, GROW)
            VALUES ('#FORM.Email#','#FORM.firstname#','#FORM.lastname#','#FORM.FUND#','#FORM.GROW#')
            </cfquery>

		   <h5>Thank You for Signing up </h5>
		   <p>You should start receiving</p> 
           <!----  cfmail goes here when ready ----->
		   <cfexit>
       
   <cfcatch>
     <cfset error = cfcatch.message>
   </cfcatch>
  </cftry>    
</cfif>


<cfif len(error)>
<div style="color:red">#error#</div>
</cfif>

Open in new window

The form now submits empty fields and there is a permanent #error# in the upper left corner.
<cfif isDefined("FORM.Submit")>

<cftry>
      <cfset FORM.Email = Trim(FORM.Email)>
         <cfif Len(FORM.Email) eq 0>
           <cfthrow message="Please enter your Email">
        </cfif> 
        
    
         <CFQUERY NAME="GetEmail" DATASOURCE="#datasource#">
           SELECT  Email
           FROM    SubscriptionIA
           WHERE email = <CFQUERYPARAM VALUE="#FORM.EMAIL#" CFSQLTYPE="CF_SQL_VARCHAR">
        </CFQUERY>

        <cfif GetEmail.recordcount GT 0>
           <cfthrow message="That email is already registered <br>to receive the newsletter">
        </cfif> 
        
	
           <cfquery  NAME="Added" datasource="#datasource#">
            INSERT INTO SubscriptionIA (Email,firstname, lastname, FUND, GROW)
            VALUES ('#FORM.Email#','#FORM.firstname#','#FORM.lastname#','#FORM.FUND#','#FORM.GROW#')
            </cfquery>

			<h5>Thank You for Signing up for our Weekly Investor Alert!</h5>
		   <p>You should start receiving the Investor Alert soon.</p> 
     
           

<!--- 		<cfmail 
		Email letter
   </cfmail> --->  
   <cfcatch>
     <cfset error = cfcatch.message>
   </cfcatch>
  </cftry>    
</cfif>  

<cfif len(error)>
<div style="color:red">#error#</div>
</cfif>

<style type="text/css">
	body {
		margin:0;
		padding:0;
		font-family:Arial, Helvetica, sans-serif;
		font-size:11px;
		color:#666666;
	}
	#wrap {
		/*width:400px;
		width:225px;
		border:1px solid #999999;*/
		text-align:left
	}
	h1 {
		margin:0;
		padding:8px 0;
		color:#003161;
		font-size:18px;
		text-align:center;
		border-bottom:1px solid #ccc;
	}
	form {
		margin:1em;
	}
	label {
		margin:0;
		font-size:10px;
		                          
	}
	form div {
		margin:.5em;
		padding:0;
		text-align:left;          
	}
	input { margin:0; padding:0;}
</style>
</head>
<body>
<div id="wrap">
	<h1>Sign Up</h1>
       
    <cfform action="#cgi.script_name#" method="POST">
 <cfif len(error)>
  <div style="position:relative;left:5px; top:2px;padding:10px; background-color:#FFFFFF;width:250px;color:maroon;text-align:right;">
 <cfoutput>#error#</cfoutput>
 </cfif> 
    <div><label>Email Address</label><br />
<cfinput type="text" name="email" id="email" size="25" /></div>	
        <div><label>First Name</label><br />
<cfinput type="text" name="firstName" id="firstName" size="25" /></div>
		<div><label>Last Name</label><br />
<cfinput type="text" name="lastName" id="lastName" size="25" /></div>
		

		<div style="text-align:right;"><cfinput type="submit" name="Submit" id="Subscribe" value="Subscribe" /></div>
	</cfform>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just removing the extra error message took care of it.  I am back to where I started. It works but does not display the message without the form showing.



<cfif len(error)>
<div style="color:red">#error#</div>
</cfif>
Did you include the cfexit tag that I said would stop processing the page after message is displayed?

I have it right after the </cfmail>, when I try to load the page it errors out with there not being a value in the email field.
>  it errors out with there not being a value in the email field

I don't know what that means, do you mean you enter an email address in theform and then the page says "Please enter an email address" or do you mean the page crashes and throws a CF error and shows the debug screen.   If so, please indicate the error and show the line.

Or do you mean there is no email in your CFMAIL tag?   The cfmail tag you are showing is incomplete, it needs from/to/subject parameters..




Ok first I am not submitting every line of code to avoid confusion.  When I try to load the page it gives the no email error - not when I try to submit the form - just trying to load the page for the first time.  The exit is trying to exit without processing the form is the only thing I can think of.
I meant "Please enter your Email"  as the error.

It doesn't make sense, the CFIF IsDefined("form.submit")  should keep the code from entering that area unless you submit the form.

<cfif isDefined("FORM.Submit")> <========== this waits for the submit =======
    <cftry>
         <cfset FORM.Email = Trim(FORM.Email)>
         <cfif Len(FORM.Email) eq 0>
              <cfthrow message="Please enter your Email">
         </cfif>

This <cfparam name="FORM.Submit" default="">
was stuck at the top from a previous attempt to get this working.  It did everything correct.

Thanks and sorry for the confusion.