aparker81
aparker81

Reputation: 263

Retain drop down list value after form submission

I have a form where a user rates a poem from 1 to 3. My code is as follows:

<select name="rating">
<cfif len(duplicateCheck.score)><option value="#duplicateCheck.score#">You scored:  #duplicateCheck.score#</option>
<cfelse><option value="">&ndash; Rate This Poem &ndash;</option>
</cfif>
<option value="1">1</option>
<option value="2">2</option>
    <option value="3">3</option>
 </select> 

If the user has already rated the poem, I am trying to make their previous score be selected. If not, the user can select 1-3. How should I do this?

Upvotes: 0

Views: 2405

Answers (2)

namtax
namtax

Reputation: 2477

If you loop through your list of options you could do this dynamically.

<cfloop from="1" to="3" index="thisOption">
   <option value="#thisOption#" <cfif userHasSelected eq thisOption> selected="selected"   
   </cfif>>#thisOption#</option>
</cfloop>

Or you can move the code to select the drop down out of the option html, which i prefer.

<cfloop from="1" to="3" index="thisOption">
   <cfset variables.selected = userHasSelected eq thisOption? 'selected' : '' />
   <option value="#thisOption#" #selected#>#thisOption#</option>
</cfloop>

Upvotes: 0

charliegriefer
charliegriefer

Reputation: 3382

Depends on how you're storing the fact that the user has already rated the poem. But from a high level:

<option value="1"<cfif userHasSelected eq 1> selected="selected"</cfif>>1</option>
<option value="2"<cfif userHasSelected eq 2> selected="selected"</cfif>>2</option>
<option value="3"<cfif userHasSelected eq 3> selected="selected"</cfif>>3</option>

So, do you already have a handle on whether or not the user has rated the poem? Or is that the actual question?

Upvotes: 2

Related Questions