Skip to main content
Known Participant
July 10, 2008
Question

How to retain form values

  • July 10, 2008
  • 4 replies
  • 3785 views
I have a form with checkboxes, radio buttons and an input for email, <input type="text" name="myemail">.
When I submit the form, I use isValidate("email', form.myemail"> to validate the entered email.

If it is invalid, I use cflocation to go back to the form and display a javascript popup message :
<script language="javascript">
alert ("Please enter a valid email address for );
email_form.myemail.focus();
</script>

The problem I am having is that if the form is filled out and I submit, then come back to the form for the error message, the fields that were filled out are all cleared and I have to start all over.

Is there a way to retain the entries and still display the popup error message ?
    This topic has been closed for replies.

    4 replies

    July 10, 2008
    You can also change your form to a cfform and use "validate" inside your input tag to avoid writing javascript.

    <cfinput type="text" name="myemail" validate="email">
    Known Participant
    July 12, 2008
    I checked and there is not validate = 'email' for cfinput, is this a new feature ?
    Inspiring
    July 10, 2008
    Try using javascript window.history.back instead of cflocation. If that doesn't work then you have to use the session variables mentioned earlier.
    Inspiring
    July 10, 2008
    dongzky wrote:
    > My suggestion is validate your form fields on the same page using javascript. This
    > way, there is no need for submission when validating your fields, which is
    > faster.
    >

    and I, with javascript disabled by default in my FF3, will be able to
    submit whatever I want with your form...

    ALWAYS validate on server.
    Add client-side validation for usability and better user experience, but
    NEVER rely on it to actually validate your data.


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/
    Inspiring
    July 10, 2008
    quote:

    Originally posted by: Newsgroup User
    dongzky wrote:
    > My suggestion is validate your form fields on the same page using javascript. This
    > way, there is no need for submission when validating your fields, which is
    > faster.
    >

    and I, with javascript disabled by default in my FF3, will be able to
    submit whatever I want with your form...

    ALWAYS validate on server.
    Add client-side validation for usability and better user experience, but
    NEVER rely on it to actually validate your data.


    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/



    good point. thanks.
    then you can use SESSION variables then.
    Inspiring
    July 10, 2008
    Ok, so this is how I understand your app. You have a form and let the validation done in another file after submission. Which means you let cf server do the validation and then use cflocation to go back to the form in case validation fails. You can actually temporarily store the field inputs in a SESSION variable. The bad thing about this approach though is that it takes some tome to validate your form since it needs a hit to the server. My suggestion is validate your form fields on the same page(the client side) using javascript. This way, there is no need for submission when validating your fields, which is faster.

    example;
    <input type="submit" name="butSubmit" value="Submit" onclick="return validateForm(this.form);">

    where validateForm() is a function which validates your field. example, for your email, maybe do something like:

    <script>
    <!--
    function validateForm(myForm)
    {
    if(validateEmail(myForm.emailField))
    {
    return true;
    }
    else
    {
    alert('invalid email');
    return false;
    }
    }

    function validateEmail(emailAd)
    {
    //do some email validations
    if(email is ok)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    //-->

    </script>
    Known Participant
    July 12, 2008
    how would I store the form entries into a session, is that done directly on form page ?
    Inspiring
    July 12, 2008
    Store them after submission. For your app or this example, in the validation page. Before you use session variables, don't forget to enable them in your cf admin and through the <cfapplication> tag. After that, maybe you can do something like this.

    Assume this is your form page, formPage.cfm:

    <!--- Default these variables if not declared yet --->
    <cfparam name="SESSION.text1" default="">
    <cfparam name="SESSION.checkbox1" default="">
    <cfparam name="SESSION.radChoice" default="">

    <cfif isdefined("SESSION.failedValidate") and SESSION.failedValidate>
    <script>
    alert("failed validation");
    </script>

    </cfif>
    <cfoutput>

    <form name="frmTemp" action="validateForm.cfm" method="post">
    text1 <input type="Text" name="text1" value="#SESSION.text1#"> <br />
    checkbox 1 <input type="Checkbox" name="checkbox1" value="check 1" <cfif SESSION.checkbox1 eq "check 1">checked</cfif>><br />
    option1 <input type="Radio" name="radChoice" value="option 1" <cfif SESSION.radChoice eq "option 1">checked</cfif>>
    option2 <input type="Radio" name="radChoice" value="option 2" <cfif SESSION.radChoice eq "option 2">checked</cfif>>
    option3 <input type="Radio" name="radChoice" value="option 3" <cfif SESSION.radChoice eq "option 3">checked</cfif>><br /><br />

    <input type="Submit" name="butSubmit" value="submit">

    </form>

    </cfoutput>


    -------------------------------------------------------------------------------------------
    Assume this is the page you validate your form, validateForm.cfm:

    <!--- IsValidateSuccessful is used to indicate if form validation succeeds or not --->
    <cfparam name="IsValidateSuccessful" default="FALSE">
    <cfif IsDefined("FORM.butSubmit")>


    <cfif Not(IsValidateSuccessful)>
    <cfset SESSION.text1=FORM.text1>
    <cfset SESSION.checkbox1=FORM.checkbox1>
    <cfset SESSION.radChoice=FORM.radChoice>
    <cfset SESSION.failedValidate = TRUE>
    <cflocation url="formPage.cfm" addtoken="No">
    <cfelse>
    <!--- delete session variables if validation is successful to free memory --->
    <cfset StructDelete(SESSION,"text1")>
    <cfset StructDelete(SESSION,"checkbox1")>
    <cfset StructDelete(SESSION,"radChoice")>
    <cfset StructDelete(SESSION,"failedValidate")>
    </cfif>

    </cfif>