Retaining user-entered values when going back to previous page.

I'm developing a program that requires users to log in (or register if they do not have an account). If the user enters invalid information (e.g. invalid password/two email addresses entered differently), they receive an error page with an "a href" link to go back to the form page. However, they lose all the information already entered. How can I retain this information when sending the user back to the login/registration page?
wpstechAsked:
Who is Participating?
 
wytcomCommented:
One way is to use hidden form fields.

Change your page that notifies them of the error to a form with a submit button that takes them back to the login page.  

When you get the submission of the login page you have the form variables submitted.  So use them to set the values of the hidden form fields in the error page.  Then when the error page is submitted, you get these values back again to use in your login page.
0
 
TeggertCommented:
<!--- Code to process login/registration goes here --->

<cfparam name="form.username" default="">
<cfparam name="form.password" default="">

<!--- Do a cfif to check for a valid login here --->
<cfif your login criteria passes>
   Your logged in/registration processing code.
<cfelse>
   <form action="" method="post">
      <input type="text" name="username" value="#form.username#">
      <input type="password" name="password" value="#form.password#">
   </form>
</cfif>


Basically that. Have your form populate with information submitted from the same form. By setting cfparams above the form, you cover for when there is no submission. In a case like this, you want to have all of your processing on the same page as the form, for ease of access. If you really must spread it across pages, wytcom has the right idea. Keep the information pushing by hidden forms. Do good initial input validation and it will work out fine.
0
 
JeffHowdenCommented:
Forms that post to themselves are far and away the most user-friendly.  Here's a super basic example:

<cfparam name="form.username" default="">

<cfif LCase(cgi.request_method) EQ "post">
  <cfscript>
    username_error = '';
    if(NOT Len(form.username))
      username_error = 'Username is required.';
    else if(Len(form.username) GT 50)
      username_error = 'Username must be 50 characters or less.';
    if(Len(username_error))
      username_error = '<p class="error">' & username_error & '</p>';
  </cfscript>
  <cfif NOT Len(username_error)>
    <!--- do all the stuff you'd do with form.username data if it were valid, then redirect --->
  </cfif>
</cfif>

<cfoutput>
  <form action="#cgi.script_name#" method="post">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" value="#form.username#" />
    #username_error#
    <input type="submit" value="Submit" />
  </form>
</cfoutput>

Obviously you'd want to break the various parts into separate templates if you have a very complex form.  When looking at it, there are pieces that are definitely part of the act of setting up the default values, querying stuff from a database, etc.  Then, there's another part that's dedicated to validation.  After that, there's a part that's responsible for processing the submitted data if it passes validation.  Finally, there's the bit that takes care of displaying HTML to the browser.  With that in mind, here are the logical divisions I use:

<!--- login.cfm --->

<cfinclude template="login/act_login.cfm">
<cfif LCase(cgi.request_method) EQ "post">
  <cfinclude template="login/val_login.cfm">
  <cfinclude template="login/pro_login.cfm">
</cfif>
<cfinclude template="login/dsp_login.cfm">

<!--- act_login.cfm --->

<cfparam name="form.username" default="">

<!--- val_login.cfm --->

<cfscript>
  valid_data = 1;
  username_error = '';
  if(NOT Len(form.username))
    username_error = 'Username is required.';
  else if(Len(form.username) GT 50)
    username_error = 'Username must be 50 characters or less.';
  if(Len(username_error))
  {
    username_error = '<p class="error">' & username_error & '</p>';
    valid_data = 0;
  }
</cfscript>

<!--- pro_login.cfm --->

<cfif NOT Val(valid_data)>
  <!--- do all the stuff you'd do with form.username data if it were valid, then redirect --->
</cfif>

<!--- dsp_login.cfm --->

<cfoutput>
  <form action="#cgi.script_name#" method="post">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" value="#form.username#" />
    #username_error#
    <input type="submit" value="Submit" />
  </form>
</cfoutput>

I've never looked back since figuring this out a long time ago.
0
 
jtreherCommented:
in cfform there is an option to preservedata="yes", give that a try too.
0
 
pcaiooCommented:
you can add the follow to your login.cfm
<cfset formhtmldata="">
<CFIF isdefined("fieldnames")>
      <cfsilent>
      <CFLOOP list="#fieldnames#" index="thisfield" delimiters=",">
            <cfif thisfield neq "login"
            and thisfield neq "password"
            and evaluate("form.#thisfield#") neq "logout">
                  <CFIF left(thisfield,1) neq val(left(thisfield,1)) and left(thisfield,1) is not "_">
                        <cfset formhtmldata=formhtmldata&'<input type="hidden" name="#trim(thisfield)#" value="#evaluate("form.#thisfield#")#">'>
                  </CFIF>
            </cfif>
      </CFLOOP>

      </cfsilent>
</CFIF>
<BODY onLoad="redirect()">
      <form method="post" action="#CGI.SCRIPT_NAME#?#CGI.QUERY_STRING#">
            #formhtmldata#
      </form>
      <SCRIPT language="javascript">
            function redirect(){
            setTimeout('document.forms[0].submit();', 100);
            }
      </SCRIPT>
</BODY>

and you can add the follow to application.cfm

<cfif not login>
<cfinclude template="login.cfm">
<cfabort>
</cfif>
then if any user not login will read only login.cfm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.