Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

Bring back good info already in a form that had an error

Hi all,
I have a form and don't want people to lose the information they already typed in once they submit their form and submit it.  I have some older documents in my application that use something like the following:

<cfoutput query="getCatalogs">
      <cfset s = iif(getCatalogs.catalog_id eq catalogId, DE("selected"), DE(""))>
      <option value="#getCatalogs.catalog_id#" #s#>#getCatalogs.catalog# (#getCatalogs.catalog_id#)

I'm fairly certain that there is a different or more modern method that this.  When someone submits their name in the form I want to return the good information back to the form along with a note about what was missing.  The form is an entry form for a contest and so there are many input's in the form.

So, if someone types in their their name in a text input, how do I make sure that they get their name back when they forget to select an age category.

If the above code is fine I can live with that although I don't completely understand what is going on in the second and third lines.
Thanks, Jerry
0
geraldw
Asked:
geraldw
  • 4
  • 2
1 Solution
 
James RodgersWeb Applications DeveloperCommented:
submit the form to itsself and process if all ok, if there are erorrs redisplay teh form with appropriate error messages
here's an example

<cfset TITLE="Human Resources - Login">    
<cfset errName = "Invalid User ID">
<cfset errPass = "Invalid Password">
<cfset bNameErr = 0>
<cfset bPassErr = 0>
<cfif isdefined("form.userName") and isDefined("form.userPass")>
          <cfquery name="validName" datasource="#SQLSERVER#" password="#SQLSERVERPASS#" username="#SQLSERVERUSER#">
               SELECT USERID
               FROM HR_TOOLBOX_USER_PROFILE
               WHERE USERLOG = '#form.userName#'
          </cfquery>
          <cfif (#validName.recordcount#)>
               <cfquery name="validPass" datasource="#SQLSERVER#" password="#SQLSERVERPASS#" username="#SQLSERVERUSER#">
                    SELECT USERID , USERPASS
                    FROM HR_TOOLBOX_USER_PROFILE
                    WHERE USERPASS = '#form.userPass#' and USERLOG = '#form.userName#'
               </cfquery>
               <cfif (#validPass.recordcount#) AND not Compare(form.userPass, validPass.userpass)>
                         <cfquery name="getUser" datasource="#SQLSERVER#" password="#SQLSERVERPASS#" username="#SQLSERVERUSER#">
                              SELECT HR_TOOLBOX_USER_PROFILE.USERID, HR_TOOLBOX_USER_PROFILE.USERLOG, HR_TOOLBOX_USER_PROFILE.USERPASS, HR_TOOLBOX_USER_PROFILE.USERFNAME, HR_TOOLBOX_USER_PROFILE.USERLNAME, HR_TOOLBOX_USER_PROFILE.USEREMAIL, Employee.GRADE AS USERGRD, HR_TOOLBOX_USER_PROFILE.USERTYPE, HR_TOOLBOX_USER_PROFILE.USERLEVEL, HR_TOOLBOX_USER_PROFILE.WARNPASS, HR_TOOLBOX_USER_PROFILE.PASSWORDUPDT, HR_TOOLBOX_USER_PROFILE.PROFILEUPDT
                              FROM HR_TOOLBOX_USER_PROFILE LEFT JOIN Employee ON HR_TOOLBOX_USER_PROFILE.EMPID = Employee.EMPID
                              WHERE USERPASS = '#form.userPass#' and USERLOG = '#form.userName#'
                         </cfquery>
                         <cfset SESSION.Auth = StructNew()>
                         <cfset SESSION.Auth.IsLoggedIn=1>
                         <cfset SESSION.Auth.UserID=#getUser.USERID#>
                         <cfset SESSION.Auth.UserLog=#getUser.USERlOG#>
                         <cfset SESSION.Auth.PassWd=#getUser.USERPASS#>
                    <cfset SESSION.Auth.FirstName=#getUser.USERFNAME#>
                         <cfset SESSION.Auth.LastName=#getUser.USERLNAME#>
                         <cfset SESSION.Auth.Email=#getUser.USEREMAIL#>
                         <cfset SESSION.Auth.Grade=#getUser.USERGRD#>
                         <cfset SESSION.Auth.AccessType=#getUser.USERTYPE#>
                         <cfset SESSION.Auth.AccessLevel=#getUser.USERLEVEL#>
                         <cfset SESSION.Auth.ShowPassWarn=#getUser.WARNPASS#>
                         <cfset SESSION.Auth.PassDate=#getUser.PASSWORDUPDT#>
                         <cfset SESSION.Auth.ProfileDate=#getUser.PROFILEUPDT#>
                         <cfset SESSION.Auth.showtopten = 0>
                         <cfif SESSION.Auth.ShowPassWarn NEQ 0>
                              <cfset SESSION.Auth.Warnings=true>
                         <cfelse>
                              <cfset SESSION.Auth.Warnings=false>
                         </cfif>
                         <cfoutput>
                              <script>
                              <cfif #getUser.USERLEVEL# GE 100>
                              window.open('#SRVPATH#HRtoolbox/start.cfm');
                              <cfelse>
                              window.open('#SRVPATH#HRtoolbox/start.cfm','','scrollbars=yes,location=no,status=no,directories=no,toolbar=no,menubar=no,resizable=no,channelmode=yes, fullscreen=yes');
                              </cfif>
                              window.location.replace('index.cfm');
                              </script>
                         </cfoutput>
               <cfelse>
                    <cfset bPassErr = 1 >
               </cfif>
          <cfelse>
               <cfset bNameErr = 1>
          </cfif>          
</cfif>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<CFHEADER name="Expires" value="#Now()#">
<CFHEADER name="Pragma" value="no-cache">
<CFHEADER name="cache-control" value="no-cache, no-store, must-revalidate">

<script type="text/javascript" language="JavaScript">
uName = /^\w*$/
uPass = /^[a-zA-Z]\w*....[^_]$/
uPassDig = /.*\d.*/
function validateData(){
err="";
var txtBox=document.tbLogin;
          if (txtBox.userName.value){
               if (!uName.test(txtBox.userName.value)){
                    if(!err){
                         txtBox.userName.focus();
                         txtBox.userName.select();
                    }
                    err = err + "User ID may contain letters, numbers \nand/or the underscore(_) and at least 6 characters.\n";
                    err = err + "User ID may not begin with underscore or a number and may not end with underscore.\n";
               }
          }
          else{
               if(!err){
                    txtBox.userName.focus();
                    txtBox.userName.select();
               }
               err = err + "User ID is a required field.\n";
          }
         
         
         
          if(txtBox.userPass.value){
               if (!uPassDig.test(txtBox.userPass.value)){
                    err = err + "Password must conatin at least 1 digit.\n";
               }
               else{
                    if (!uPass.test(txtBox.userPass.value)){
                         if(!err){
                              txtBox.userPass.focus();
                              txtBox.userPass.select();
                         }
                         err = err + "Password may contain letters, numbers \nand/or the underscore(_) and be at least 6 characters.\n";
                         err = err + "Password may not begin with underscore or a number and may not end with underscore.\n";
                    }
                   
               }
          }
          else{
               if(!err){
                         txtBox.userPass.focus();
                         txtBox.userPass.select();
                    }
               err = err + "Password is a required field.\n";
          }
         

          if(txtBox.userPass.value && txtBox.userName.value && txtBox.userPass.value == txtBox.userName.value){
               err = err + "User ID and Password cannot be the same.\n";
          }
         
          if (err){
               alert(err);
          }    

return (err ? false : true);    
}
</script>


<table width="150" border="0" cellspacing="0" cellpadding="0" align="right">
  <tr>
    <td width="150" height="15" colspan="2" align="center" bgcolor="#9DA2C8"><font face="Arial, Helvetica, sans-serif" size="2">H.R. Toolbox</font> </td>
  </tr>
  <tr height="30">
    <form action="index.cfm" method="post" name="tbLogin" onsubmit="return validateData();">
      <tr>
        <td width="150" height="10" colspan="2" align="left" bgColor="#fcf1f2">&nbsp;<font color="#8b0000" face="Arial, Helvetica, sans-serif" size="2">
          <a href="##" onClick="window.open('eis/hr_toolbox/toolboxRegistration.cfm','News','scrollbars=no,width=400,height=400,resizable=no')" class="bottomnav">New User? Click to Register</A></font></td>
      </tr>
       <cfif (#bNameErr#) eq 1>
       <tr>              
          <td width="150" height="10" colspan="2" style="font: small-caps bold 10pt; color:red;" align="center" bgColor="#fcf1f2">Invalid User ID</td>
       </tr>          
       <cfelseif (#bPassErr#) eq 1>
       <tr>              
          <td width="150" height="10" colspan="2" style="font: small-caps bold 10pt; color:red;" align="center" bgColor="#fcf1f2">Invalid Password. Password is case SENSITIVE</td>
       </tr>
     <cfelse>
     
     </cfif>          
         
      <tr>
        <td height="10" width="72" bgColor="#fcf1f2" align="left">&nbsp;<font color="#8b0000" face="Arial, Helvetica, sans-serif" size="2">User ID:</font></td>
        <td width="73" bgColor="#fcf1f2" align="left"><font size="3"><input name="userName"  type="text" size="8" maxlength="20" <cfif isDefined("form.userName") and not bNameErr and not isDefined("SESSION.Auth.IsLoggedIn")><cfoutput> value="#form.userName#"</cfoutput></cfif>></font></td>
      </tr>
      <tr>
        <td width="72" height="10" align="left" bgColor="#fcf1f2">&nbsp;<font color="#8b0000" face="Arial, Helvetica, sans-serif" size="2">Password:</font></td>
        <td width="73" height="10" align="left" bgColor="#fcf1f2"><font size="3"><input name="userPass" type="password" size="8" maxlength="20"></font></td>
      </tr>
      <tr>
        <td width="150" height="10" colspan="2" align="right" bgColor="#fcf1f2">    
        <font color="#8b0000" face="Arial, Helvetica, sans-serif" size="2"><input align="right" name="submit" type="submit" value="Log In">&nbsp;&nbsp;</font></td>
      </tr>
    </form>
  <tr>
    <td width="150" height="10" colspan="2" bgColor="#fcf1f2"><font color="#8b0000" face="Arial, Helvetica, sans-serif" size="2">
     &nbsp;<a href="##" onClick="window.open('eis/hr_toolbox/password_request.cfm','News','scrollbars=no,width=400,height=400,resizable=no')" class="bottomnav">Lost or Forgot Password?</A></font></td>
  </tr>
  <tr>
    <td width="150" height="15" colspan="2"  valign="middle" bgcolor="#9DA2C8">&nbsp;</td>
  </tr>

  <tr><td width="150" height="15" colspan="2"  align="center">

       </td>
  </tr>
</table>
 
0
 
pinaldaveCommented:
Hi geraldw,
Very correct Answer from Jester_48.
following is the method which I use. if sucess it goes to other page or it will come to same page with incorrect form value. as you have asked. Do not forget to surrange the code with apropriate place wiht <cfoutput>

<cfif isdefined('formname.submit')>
<cfquery name="queryname">
your sql statement
</cfquery>
<cfif queryname.recordcount >
<cflocation url="sucess page">
<cfelse>
There is error.
</cfif>

</cfif>

<form action="" method="post" name="formname">
<input type="text" value="#form.textname#" name="textname">
<input type="submit" value="submit" name="submit">
</form>


Regards,
---Pinal
0
 
James RodgersWeb Applications DeveloperCommented:
An explanation and a code sample....

grade B?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
geraldwAuthor Commented:
Hi Jester48
I don't intend to be insulting or anything. I'm just having some difficulty pulling out the little bit of code I need from what you posted.  So yes, thanks much for a thorough code example but the only reason I didn't give an A is because there is so much more there to wade through than I probably needed.  It's neat to have a chance to peer into the code of someone who obviously really knows what they are doing - for someone like myself who's only so-so it's a little daunting trying to figure out exactly what I need.
Thanks again, Jerry
0
 
James RodgersWeb Applications DeveloperCommented:
follow up q's are always welcome, if there was something you needed clarification on i would have been more than happy to go through the code, line by line, if need be.

<!--- errro messages and boolean checks --->
<cfset errName = "Invalid User ID">
<cfset errPass = "Invalid Password">
<cfset bNameErr = 0>
<cfset bPassErr = 0>

<!--- double submission validation 1-validate user id/name 2- if valid user validate user id/name AND password --->
<!--- i use double validation so that i can let the user know whether the login error was due to invalid user id/name or invalid password --->
<!--- this lets the user know where they have made the error and correct as necessary --->
<cfif isdefined("form.userName") and isDefined("form.userPass")>
          <cfquery name="validName" datasource="#SQLSERVER#" password="#SQLSERVERPASS#" username="#SQLSERVERUSER#">
               SELECT USERID
               FROM HR_TOOLBOX_USER_PROFILE
               WHERE USERLOG = '#form.userName#'
          </cfquery>
          <cfif (#validName.recordcount#)>
               <cfquery name="validPass" datasource="#SQLSERVER#" password="#SQLSERVERPASS#" username="#SQLSERVERUSER#">
                    SELECT USERID , USERPASS
                    FROM HR_TOOLBOX_USER_PROFILE
                    WHERE USERPASS = '#form.userPass#' and USERLOG = '#form.userName#'
               </cfquery>



<!--- if user login was successful read user information from user profile table in data base --->
<cfif (#validPass.recordcount#) AND not Compare(form.userPass, validPass.userpass)>
                         <cfquery name="getUser" datasource="#SQLSERVER#" password="#SQLSERVERPASS#" username="#SQLSERVERUSER#">
                              SELECT HR_TOOLBOX_USER_PROFILE.USERID, HR_TOOLBOX_USER_PROFILE.USERLOG, HR_TOOLBOX_USER_PROFILE.USERPASS, HR_TOOLBOX_USER_PROFILE.USERFNAME, HR_TOOLBOX_USER_PROFILE.USERLNAME, HR_TOOLBOX_USER_PROFILE.USEREMAIL, Employee.GRADE AS USERGRD, HR_TOOLBOX_USER_PROFILE.USERTYPE, HR_TOOLBOX_USER_PROFILE.USERLEVEL, HR_TOOLBOX_USER_PROFILE.WARNPASS, HR_TOOLBOX_USER_PROFILE.PASSWORDUPDT, HR_TOOLBOX_USER_PROFILE.PROFILEUPDT
                              FROM HR_TOOLBOX_USER_PROFILE LEFT JOIN Employee ON HR_TOOLBOX_USER_PROFILE.EMPID = Employee.EMPID
                              WHERE USERPASS = '#form.userPass#' and USERLOG = '#form.userName#'
                         </cfquery>

<!--- create a session variable as a structure to contain user information to be used throughout the application --->
<!--- most developers will not have this type/quantity of information --->
                         <cfset SESSION.Auth = StructNew()>
                         <cfset SESSION.Auth.IsLoggedIn=1>
                         <cfset SESSION.Auth.UserID=#getUser.USERID#>
                         <cfset SESSION.Auth.UserLog=#getUser.USERlOG#>
                         <cfset SESSION.Auth.PassWd=#getUser.USERPASS#>
                    <cfset SESSION.Auth.FirstName=#getUser.USERFNAME#>
                         <cfset SESSION.Auth.LastName=#getUser.USERLNAME#>
                         <cfset SESSION.Auth.Email=#getUser.USEREMAIL#>
                         <cfset SESSION.Auth.Grade=#getUser.USERGRD#>
                         <cfset SESSION.Auth.AccessType=#getUser.USERTYPE#>
                         <cfset SESSION.Auth.AccessLevel=#getUser.USERLEVEL#>
                         <cfset SESSION.Auth.ShowPassWarn=#getUser.WARNPASS#>
                         <cfset SESSION.Auth.PassDate=#getUser.PASSWORDUPDT#>
                         <cfset SESSION.Auth.ProfileDate=#getUser.PROFILEUPDT#>
                         <cfset SESSION.Auth.showtopten = 0>
                         <cfif SESSION.Auth.ShowPassWarn NEQ 0>
                              <cfset SESSION.Auth.Warnings=true>
                         <cfelse>
                              <cfset SESSION.Auth.Warnings=false>
                         </cfif>
                         <cfoutput>
                              <script>

<!--- at this point a check is made to vlaidate the user access level, >=100 is admin all others are regular users, open the appropriate style window  --->
                              <cfif #getUser.USERLEVEL# GE 100>
                              window.open('#SRVPATH#HRtoolbox/start.cfm');
                              <cfelse>
                              window.open('#SRVPATH#HRtoolbox/start.cfm','','scrollbars=yes,location=no,status=no,directories=no,toolbar=no,menubar=no,resizable=no,channelmode=yes, fullscreen=yes');
                              </cfif>

<!--- as teh form submits to itsself the user, or someone else,  could refresh the screen and relogin this javascript code block replaces teh form submission page with standard start page (as per my site) removing teh form submission from the browser cache/history --->
                              window.location.replace('index.cfm');
                              </script>
                         </cfoutput>

<!--- if user id/name and password check is invalid set password error boolean to true --->
               <cfelse>
                    <cfset bPassErr = 1 >
               </cfif>

<!--- if user id/name check is invalid set id/name error boolean to true --->
          <cfelse>
               <cfset bNameErr = 1>
          </cfif>          
</cfif>


<!--- set cf headers to prevent page caching --->
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<CFHEADER name="Expires" value="#Now()#">
<CFHEADER name="Pragma" value="no-cache">
<CFHEADER name="cache-control" value="no-cache, no-store, must-revalidate">


0
 
geraldwAuthor Commented:
Hi Jester48
Thanks for the clarification.  I didn't realize the grade was so important.  I thought it was the points that mattered. Nonetheless, I didn't want to keep bombarding you with questions as I thought that would be more annoying to you.
Jerry
0
 
James RodgersWeb Applications DeveloperCommented:
>> I didn't want to keep bombarding you with questions as I thought that would be more annoying to you.
if i was worried about follow up questions i wouldn't post. Just because a code sample is posted doesn't mean it's a final answer, never accept something just because it looks good, or has a lot of code follow up ask until you are satisfied, i think most of the experts that answer postings would agree with those sentiments... think of an asker as a customer and teh experts are teh people here to serve your needs  
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now