Solved

CF - Using cflocation with form submit

Posted on 2011-03-24
18
924 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:JohnMac328
  • 9
  • 9
18 Comments
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
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>

0
 

Author Comment

by:JohnMac328
Comment Utility
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
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
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

0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
> 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..

0
 

Author Comment

by:JohnMac328
Comment Utility
So the cfexit would send the mail, not show the form and display the message?
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
> 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

0
 

Author Comment

by:JohnMac328
Comment Utility
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

0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
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

0
 

Author Comment

by:JohnMac328
Comment Utility
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

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
Comment Utility
This appears to be removed from the code

<cfset error = "">

It is necessary when there is no form post, not sure why it's not giving you an error.  Perhaps it's there, just not included in the post


You included this error display inside your form, so you can removed this redundant code

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


You were not closing two of the DIV tags

<div id="wrap">  <============= this was never closed
 <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;">  <============= this was never closed
     <cfoutput>#error#</cfoutput>


Here is the updated code.

What do you mean the form is submitting empty fields?   Are you saying it is populating the database will all empty values even when an email address is Entered?
<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    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>
     </div> 
  </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>
</div>

Open in new window

0
 

Author Comment

by:JohnMac328
Comment Utility
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>
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
Did you include the cfexit tag that I said would stop processing the page after message is displayed?

0
 

Author Comment

by:JohnMac328
Comment Utility
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.
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility
>  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..




0
 

Author Comment

by:JohnMac328
Comment Utility
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.
0
 

Author Comment

by:JohnMac328
Comment Utility
I meant "Please enter your Email"  as the error.
0
 
LVL 39

Expert Comment

by:gdemaria
Comment Utility

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>

0
 

Author Closing Comment

by:JohnMac328
Comment Utility
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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now