?
Solved

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

Posted on 2006-05-02
8
Medium Priority
?
334 Views
Last Modified: 2013-12-24
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?
0
Comment
Question by:wpstech
5 Comments
 
LVL 7

Accepted Solution

by:
wytcom earned 128 total points
ID: 16589763
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
 
LVL 4

Assisted Solution

by:Teggert
Teggert earned 124 total points
ID: 16593096
<!--- 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
 
LVL 7

Assisted Solution

by:JeffHowden
JeffHowden earned 124 total points
ID: 16594433
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
 
LVL 5

Assisted Solution

by:jtreher
jtreher earned 124 total points
ID: 16596405
in cfform there is an option to preservedata="yes", give that a try too.
0
 
LVL 3

Expert Comment

by:pcaioo
ID: 16616392
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

830 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