Link to home
Start Free TrialLog in
Avatar of dndco
dndco

asked on

Form Validaton, Select Menus Losing Session Info

I have a two page processing set up, form and action page with error checking for required fields.  All fields are stored in a session but I am having sort of two problems with select menus.

First off, if a user missed a required field on the form page when they submitted to the action/processing page, select menu values that were stored in the session did not update themselves. So if a user had chosen the state of California on the form page, on the action/processing page it was back to the default 'Choose A State'. This was the case for all select menus on the page. To altered the select menus on the action page as such;

<cfselect
          name="State"          
          required="yes"
          message="Please select your State">
              <OPTION value="#session.buynow_nfo.State#" selected><cfoutput>#session.buynow_nfo.State#</cfoutput></OPTION>
              <option value="Alabama">Alabama</option>
              <option value="Alaska">Alaska</option>
              <option value="Arizona">Arizona</option>
              <option value="Arkansas">Arkansas</option>
              <option value="California">California</option>
               etc....
</cfselect>

Which worked, or so I thought.  If the user makes an error in a text field or select menu, select menus that have been selected did update with the session info passed to the action page.  However, if a user makes TWO mistakes and therefore submits the form twice all select boxes lose the session info and display the #session.buynow_nfo.NAMEOFFIELD# fix, I thought I had created.

This is not seen as an error, so when and if all the required text fields are correct the form submits and the select values all come through with #session.buynow_nfo.NAMEOFFIELD#

How can I fix this?
Avatar of dgrafx
dgrafx
Flag of United States of America image

I believe you need to do something like:

<cfoutput>
<select name="State">
<cfloop list="#statelist#" index="ii">
<option value="#ii#" <cfif ii is session.buynow_nfo.State>selected</cfif>>#ii#
</cfloop>
</select>
</cfoutput>
Avatar of dndco
dndco

ASKER

Thanks anyway, but it looks like you are pulling the #statelist# from a DB and looping through to the next item with "ii".  I don't have a database for this.   I just have the 50 states coded in the form.  

However, your code did give me an idea.  I wrapped the selected option value with cfoutput like this;

<cfselect
          name="State"          
          required="yes"
          message="Please select your State">
              <cfoutput>
              <OPTION value="#session.buynow_nfo.State#" selected>#session.buynow_nfo.State#</OPTION>
             </cfoutput>
              <option value="Alabama">Alabama</option>
              <option value="Alaska">Alaska</option>
              <option value="Arizona">Arizona</option>
              <option value="Arkansas">Arkansas</option>
              <option value="California">California</option>
               etc....
</cfselect>

And it works perfectly now.  

ASKER CERTIFIED SOLUTION
Avatar of dgrafx
dgrafx
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dndco

ASKER

I am happy with the results, thanks.

I'll be posting a new question tomorrow.  Another problem with the same form.