ColdFusion Programming/Retain form values
Expert: Donald Hammond - 3/9/2009
QuestionQUESTION: I have a form A that the user fills out. If they submit, it inserts the information into a table. However, there is an icon on the form that will take them to form B. Once form B is filled out and submittted, they are returned to form A. However, all of the entries are gone. How do I retain the entries on form A if they do not submit and go to form B, then return to form A ?
ANSWER: Aloha,
The only way to do this is to store the form data in variables and send it back to form A.
You could take the form structure from A and store it in a session variable when they get to form B.
<cfset session.formA = StructNew()>
<cfset session.formA = #StructAppend(session.formA,form)#>
Then when they go back to form A, you will have to pull the data from the structure and insert it into the forms. You will need to do "IF" statements to do this.
<input type="text" name="First_Name" value="<cfif isdefined(session.formA.first_name and session.formA.first_name IS NOT "">#session.formA.first_name#</cfif>">
Hope this helps.
---------- FOLLOW-UP ----------
QUESTION: Thanks for the response.
So form A would have this :
<cfset session.formA = StructNew()>
<cfset session.formA = #StructAppend(session.formA,form)#>
<input type="text" name="First_Name" value="<cfif isdefined(session.formA.first_name and session.formA.first_name IS NOT "">#session.formA.first_name#</cfif>">
If I enter the first_name and do not submit and then go to form B, and when I return to form A, the first name will still be sitting in the input field ?
AnswerNo. You might be able to do that in Javascript but not in Coldfusion. CF is a SERVER side scripting language which means it has to go to the server for anything to happen. Thus you must submit the form for the contents to be saved. What you could do is have a button that says "go to form b" and THAT would submit the form A and save the contents.
In Javascript you could have "onBlur()" set a cookie with the value that was entered. The onBlur means that the form field rec'd focus and then lost it as the visitor moved on to another part of the form or another page. Javascript is a CLIENT side scripting language so it can deal with browser based actions.