Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to retain form values

New Here ,
Jul 09, 2008 Jul 09, 2008
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 ?
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 09, 2008 Jul 09, 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 11, 2008 Jul 11, 2008
how would I store the form entries into a session, is that done directly on form page ?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 12, 2008 Jul 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 13, 2008 Jul 13, 2008
thanks for the reply, dongzky,

I think I understand what you are doing.

A couple of questions. Instead of having a form page and and action page, my mgmt only wants one page, so the form page has to submit back to itself. I will have all teh action page processing above the form/cfform statment, and the html form below the form/cfform statement.
Will your example still work and is the form variable still treated as if it was submitted to another page ?

Also, if I have multiple checkboxes and they all have the same name, how will that work with your example ?

thanks
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 13, 2008 Jul 13, 2008
Olivia Crazy Horse wrote:
>Instead of having a form page and and action page, my mgmt only wants one page, so the form page has to
>submit back to itself. I will have all teh action page processing above the form/cfform statment, and the html form
>below the form/cfform statement.
>Will your example still work and is the form variable still treated as if it was submitted to another page ?

Yes, the form variables will still be treated as if it was submitted to another page. But in this case, you don't need to store your form values in SESSION variables. All you need to do is do a little tweaking in your form page. Example:
Instead of declaring <cfparam name="SESSION.text1" default="">, change it to <cfparam name="FORM.text1" default="">. So all the SESSION.text1 in your form page has to be changed into FORM.text1


Olivia Crazy Horse wrote:
> if I have multiple checkboxes and they all have the same name, how will that work?

checkboxes with same names, when submitted and if there are any selections, will return a comma-delimited list. so if you want to do some checkings on a checkbox, you might have a code similar to this:

<input type="Checkbox" name="myCheckBox" value="value 1" <cfif ListFindNoCase(FORM.myCheckBox,"value 1",",")>checked</cfif>> Checkbox 1 <br />
<input type="Checkbox" name="myCheckBox" value="value 2" <cfif ListFindNoCase(FORM.myCheckBox,"value 2",",")>checked</cfif>> Checkbox 2
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2008 Jul 16, 2008
I need to go back to the original method of having a form page and an action. I tried to incorporate your original code and cannot seem to get it to work.

Here is what I have on my form page, three checkboxes side by side, checkbox name is fault (I am only testing the first one now)

<cfparam name="session.fault" default="">

<cfif isDefined("session.failedValidate") and session.failedValidate>
<script>
alert("failed validateion")
</script>
</cfif>


<input type="checkbox" name="fault" value="Supplier" <cfif session.fault is "Supplier">checked</cfif>> Supplier  

<input type="checkbox" name="fault" value="Raytheon"> Company  

<input type="checkbox" name="fault" onClick="showTextboxes(1);" value="Carrier"> Carrier

Here is my action page :

<cfparam name="IsValidateSuccessful" default="FALSE">
<cfif IsDefined("FORM.butSubmit")>


<cfif Not(IsValidateSuccessful)>

<cfset SESSION.fault=FORM.fault>

<cfset SESSION.failedValidate = TRUE>
<cflocation url="disposition_urdn.cfm" addtoken="No">
<cfelse>
<!--- delete session variables if validation is successful to free memory --->

<cfset StructDelete(SESSION,"fault")>

<cfset StructDelete(SESSION,"failedValidate")>
</cfif>

</cfif>

I check the first checkbox. There are other checkboxes beneath this one, and if those are not checked, then I return to the form page with a popup error message, hoping to retain the first checkbox check. I just do a cfif not isDefined("next_box_name")>, then cflocation url back to the form page, but the check is not there.

What am I doing wrong ?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 22, 2008 Jul 22, 2008
LATEST
Have you already enabled session variables in the CF admin? Also, did you already create a <cfapplication> tag, maybe in an Application.cfm, that is set to use SESSION variables? If not, and if you don't know yet how, check the links below:

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=sharedVars_10.html

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_a-b_5.html#1097308
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 09, 2008 Jul 09, 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/
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 10, 2008 Jul 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 10, 2008 Jul 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 10, 2008 Jul 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">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 11, 2008 Jul 11, 2008
I checked and there is not validate = 'email' for cfinput, is this a new feature ?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources