Connecting Tech Pros Worldwide Forums | Help | Site Map

Need cfset "errorMsg" to display

Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#1: Mar 16 '09
Not sure how many form submissions that have been lost over the years before I started but the company has a contact form that the required fields when validation fails the error message is going into the URL string on a refresh of the form page.

I am very green on CF and CFM files and have this code below; I need to help for the error messages to "pop up" in a new window or display on the body of the page so anyone filling out the form knows that an error occurred with the entry trying to be submitted.

Any help would be awesome!


mailer.cfm: (VALIDATE/SEND)

Expand|Select|Wrap|Line Numbers
  1. <META HTTP-EQUIV="Refresh"
  2. CONTENT="5; URL=http://whoscalling.com/index.php"> 
  3.  
  4.  
  5. <cfif IsDefined('form.send_email')>
  6.     <cfset errorMsg = ''>
  7.  
  8.     <cfif Len(Trim(form.name)) EQ 0>
  9.         <cfset errorMsg = errorMsg & '- Please enter your name<br/>'>
  10.     </cfif>
  11.     <cfif Len(Trim(form.email)) EQ 0>
  12.         <cfset errorMsg = errorMsg & '- Please enter your email address<br/>'>
  13.     <cfelseif form.email DOES NOT CONTAIN '@' OR form.email DOES NOT CONTAIN '.'>
  14.         <cfset errorMsg = errorMsg & '- Please enter a valid email address<br/>'>
  15.     </cfif>
  16.     <cfif Len(Trim(form.comments)) EQ 0>
  17.         <cfset errorMsg = errorMsg & '- Please enter your comments or questions<br/>'>
  18.     </cfif>
  19. <cfif Len(Trim(form.phone)) EQ 0>
  20.         <cfset errorMsg = errorMsg & '- Please enter 3 digit area code<br/>'>
  21.     </cfif>
  22. <cfif Len(Trim(form.phone2)) EQ 0>
  23.         <cfset errorMsg = errorMsg & '- Please enter the first three numbers of your phone number<br/>'>
  24.     </cfif>
  25. <cfif Len(Trim(form.phone3)) EQ 0>
  26.         <cfset errorMsg = errorMsg & '- Please enter the last four numbers of your phone number<br/>'>
  27.     </cfif>
  28.  
  29.  
  30.     <cfif Len(Trim(errorMsg)) GT 0>
  31.         <cfset urlString = URLEncodedFormat(errorMsg)>
  32.         <cfset valueString = "name=#URLEncodedFormat(form.name)#&company=#URLEncodedFormat(form.company)#&email=#URLEncodedFormat(form.email)#&phone=#URLEncodedFormat(form.phone)#&comments=#URLEncodedFormat(form.comments)#">
  33.         <cflocation url="#cgi.HTTP_REFERER#?email_error=1&error_msg=#urlString#&#valueString#">
  34.     <cfelse>
  35.         <cfmail to="#form.to_email#" from="#form.email#" subject="#form.subject#">
  36. An email has been submitted to learn more about FleetQUEST360 from the following person:
  37.  
  38. Name: #form.name#        
  39. Company: #form.company#
  40. Email: #form.email#
  41. Phone: #form.phone# #form.phone2# #form.phone3#
  42.  
  43. Comments: #form.comments#
  44.         </cfmail>
  45.         <cflocation url="http://www.whoscalling.com/ThankYou_fleetquest360.htm">
  46.     </cfif>
  47. <cfelse>
  48.     <p style="font:bold 12px Verdana, Arial, Helvetica, sans-serif; color:red;">You do not have access to view this page.</p>
  49. </cfif>


FORM:

Expand|Select|Wrap|Line Numbers
  1. <input type="hidden" name="to_email" value="incomingleads@xxx.com">
  2.                     <input type="hidden" name="subject" value="Sales Support">
  3.  
  4. <form action="http://xxx.com/mailer.cfm" method="post" class="style15">
  5. <table border="0" cellpadding="3" cellspacing="0" class="contact-form">
  6. <tr>
  7.                             <td>NAME:<br />
  8.                             <input name="name" type="text" class="text" value="" size="25" /></td>
  9.                             <td rowspan="4"><div align="right"><a href="javascript:makeClickToTalk('https://xxx.com/','5555555555','clickDemo')" target="_top"><img src="CLICK.gif" alt="Click To Talk" name="clicktotalk" width="200" height="134" border="0"  class="custombutton" id="clicktotalk" style="" /></a></div></td>
  10.         </tr>
  11.                         <tr>
  12.                             <td>COMPANY:<br />
  13.                               <input name="company" type="text" class="text" value="" size="25" /></td>
  14.                   </tr>
  15.                         <tr>
  16.                             <td> BUSINESS:<br />
  17.                               <select name="Business" input type="option" name="name" value="">
  18.                               <option value=""> </option>
  19.                               <option value="ad agency">Ad Agency</option>
  20.                               <option value="auto dealership">Auto Dealership</option>
  21.                               <option value="education">Education</option>
  22.                               <option value="financial">Financial</option>
  23.                               <option value="health/fitness center">Health/Fitness Center</option>
  24.                               <option value="home builders/contractors">Home Builders/Contractors</option>
  25.                               <option value="home improvement">Home Improvement</option>
  26.                               <option value="insurance">Insurance</option>
  27.                               <option value="land developer">Land Developer</option>
  28.                               <option value="legal">Legal</option>
  29.                               <option value="medical">Medical</option>
  30.                               <option value="motorcycle dealer">Motorcycle Dealer</option>
  31.                               <option value="multi-housing industry">Multi-Housing Industry</option>
  32.                               <option value="publisher">Publisher</option>
  33.                               <option value="real estate">Real Estate</option>
  34.                               <option value="retail">Retail</option>
  35.                               <option value="rv dealer">RV Dealer</option>
  36.                               <option value="sports &amp; entertainment">Sports &amp; Entertainment</option>
  37.                               <option value="transportation">Transportation</option>
  38.                               <option value="travel">Travel</option>
  39.                               <option value="watercraft dealer">Watercraft Dealer</option>
  40.                               <option value="other">Other</option>
  41.                               </select></td>    
  42. <td></td>
  43. </tr>
  44.                         <tr>                    
  45.                             <td colspan="2"> E-MAIL:<br />
  46.                               <input name="email" type="text" class="text" value="" size="25" /></td>
  47. <td></td>
  48.                         </tr>
  49.                         <tr>
  50.                           <td colspan="2" align="left">PHONE NUMBER:<br />
  51.                                                 <input name="phone" type="text" class="text" value="" size="3" maxlength="3" />
  52.                                                 <input name="phone2" type="text" class="text" value="" size="3" maxlength="3" />
  53.                                                 <input name="phone3" type="text" class="text" value="" size="4" maxlength="4" />                                                
  54.                                                 </tr>
  55.                         <tr>
  56.                           <td colspan="2" align="left">HOW CAN WE HELP?<br />
  57.                       <textarea name="comments" cols="55" rows="5"></textarea>                      </tr>
  58.                         <tr>
  59.                       <td  colspan="2" align="left"><input type="submit"  class="custombutton" value="" name="send_email" style="background-image:url(submit.jpg); width:75px; height:31px; border:0;"  />                      </tr>
  60.                     </table>            
  61.     </form>
best answer - posted by acoder
OK, from line 30-33, you have:
Expand|Select|Wrap|Line Numbers
  1.    <cfif Len(Trim(errorMsg)) GT 0>
  2.         <cfset urlString = URLEncodedFormat(errorMsg)>
  3.         <cfset valueString = "name=#URLEncodedFormat(form.name)#&company=#URLEncodedFormat(form.company)#&email=#URLEncodedFormat(form.email)#&phone=#URLEncodedFormat(form.phone)#&comments=#URLEncodedFormat(form.comments)#">
  4.         <cflocation url="#cgi.HTTP_REFERER#?email_error=1&error_msg=#urlString#&#valueString#">
Change that to:
Expand|Select|Wrap|Line Numbers
  1.    <cfif Len(Trim(errorMsg)) GT 0>
  2.         You have the following errors:<br>
  3.         <cfoutput>#errorMsg#</cfoutput>
  4.         Please correct them.
You could, of course, add some HTML/CSS to improve the display (position, colours, etc.)

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Mar 17 '09

re: Need cfset "errorMsg" to display


You could use the information that's added to the URL to display the error messages on the next page that cflocation relocates to. You would check for url.email_error. If it's defined and set to 1, then there was an error and you could parse the remaining string to get the error messages.

Having said that, that would be one step too many. It would be better to display on the page where the errors are discovered during validation. To display, just use cfoutput.
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#3: Mar 17 '09

re: Need cfset "errorMsg" to display


Quote:

Originally Posted by acoder View Post

Having said that, that would be one step too many. It would be better to display on the page where the errors are discovered during validation. To display, just use cfoutput.


I have no doubt you are an expert at this, I am less than novice. I want to learn and do best from example.

Could you please show me what I need to do to use "cfoutput" in this situation.

Cheers,

confusedfusion
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Mar 17 '09

re: Need cfset "errorMsg" to display


OK, from line 30-33, you have:
Expand|Select|Wrap|Line Numbers
  1.    <cfif Len(Trim(errorMsg)) GT 0>
  2.         <cfset urlString = URLEncodedFormat(errorMsg)>
  3.         <cfset valueString = "name=#URLEncodedFormat(form.name)#&company=#URLEncodedFormat(form.company)#&email=#URLEncodedFormat(form.email)#&phone=#URLEncodedFormat(form.phone)#&comments=#URLEncodedFormat(form.comments)#">
  4.         <cflocation url="#cgi.HTTP_REFERER#?email_error=1&error_msg=#urlString#&#valueString#">
Change that to:
Expand|Select|Wrap|Line Numbers
  1.    <cfif Len(Trim(errorMsg)) GT 0>
  2.         You have the following errors:<br>
  3.         <cfoutput>#errorMsg#</cfoutput>
  4.         Please correct them.
You could, of course, add some HTML/CSS to improve the display (position, colours, etc.)
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#5: Mar 17 '09

re: Need cfset "errorMsg" to display


Thank you for your time on this; I will give it all a try and will for sure reply back whith the success I am confident you have shown me.
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#6: Mar 17 '09

re: Need cfset "errorMsg" to display


I am trying this:

Expand|Select|Wrap|Line Numbers
  1. <cfif Len(Trim(errorMsg)) GT 0> 
  2.         <p style="font:bold 12px Verdana, Arial, Helvetica, sans-serif; color:red;">You have the following errors:<br> 
  3.         <cfoutput>#errorMsg</cfoutput> 
  4.         Please correct them. </p>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#7: Mar 17 '09

re: Need cfset "errorMsg" to display


You forgot the closing # sign:
Expand|Select|Wrap|Line Numbers
  1. <cfoutput>#errorMsg#</cfoutput>
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#8: Mar 17 '09

re: Need cfset "errorMsg" to display


Thanks for catching that for me; I have a nightmare work inviroment here; not testing server; I have to make code changes and submit online forms to have the files "pushed" live from a ftp. So I was able to fix that before submitting the request. It makes my learnig curve shoot out into the heliosphere. I have to wait to see if my code works or not.

Again, thank you.
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#9: Mar 17 '09

re: Need cfset "errorMsg" to display


The error msg worked great; it displayed and then after a few seconds it redirects to the home page, I used the back button once took me back to the displayed errors, second back took me to the form with the fields still filled out,

Can you show me a way to have the errors display and then instead of going to the home page, returning back to the form without clearing the fields?

Thank you,

C
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#10: Mar 17 '09

re: Need cfset "errorMsg" to display


Quote:

Originally Posted by confusedfusion View Post

Can you show me a way to have the errors display and then instead of going to the home page, returning back to the form without clearing the fields?


I found the "refresh" easy fix there, just changed it to the form URL; I guess the form will be blanked on the refresh but it should be fine, if the user is not filling in the blanks right then the error will tell them and they can try again.

I know I like forms that don't blank my entries on error but if mine is that way I'll just live with it.

Thanks for everything.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#11: Mar 17 '09

re: Need cfset "errorMsg" to display


How about a link back to the form (if the user doesn't know they should go back)? That'd be better than a refresh which might not give the user enough time to read all the errors. They can then change the fields and re-submit the form.

Another option is to use the same page for submission, but display the error messages and re-display the form with the fields filled in as input.
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#12: Mar 17 '09

re: Need cfset "errorMsg" to display


Quote:

Originally Posted by acoder View Post

Another option is to use the same page for submission, but display the error messages and re-display the form with the fields filled in as input.

I like your suggestion of Opt2 but not sure how to get that to work.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#13: Mar 17 '09

re: Need cfset "errorMsg" to display


Instead of setting the form action to mailer.cfm, set it to the same page and put the code in mailer.cfm in the form page. Of course, at the top, it should check that if anything has been submitted, e.g.
Expand|Select|Wrap|Line Numbers
  1. <cfif isdefined("form.send_email")>
  2. <!--- mailer code here --->
  3. </cfif>
Then for the form, you can set all the values to empty initially, but when submit (with errors), set their values to the inputted values, e.g. for a text field:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" ... value="<cfoutput>#phone#</cfoutput>">
That's the basic idea and there are other ways you could do this too. Also, if you just want a basic, quick easy solution, then the back link would be best.
Newbie
 
Join Date: Mar 2009
Location: TEXAS
Posts: 15
#14: Mar 17 '09

re: Need cfset "errorMsg" to display


That looks good, I may or may not go the "quick and easy" with a back button; It may not be good work habits on my part but at around 18K £ a year not going to go too much farther out to gets this page "done."

Thanks for the awesome expert information. I will rember it and will also answer what I can from time to time here at bytes.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#15: Mar 18 '09

re: Need cfset "errorMsg" to display


No problem. Since you're new to Coldfusion, perhaps you'll find these tutorials useful. Also, have a look at the Adobe website for tutorials, guides and documentation.
Reply