Question

I want to submit my form, then have the form self-reference itself and retain the values.

How can I set it up to retain which item I have selected? It can retain the value of the selected item upon the second form load? All I know is that I may have to do a CFloop rather than run my query directly on the query attribute.

Heres my select code:

<cfselect Name="CandyLand" required="yes" size="6" 
Query="getCandyInfo" style="width: 200px" 
Value="UserID"  Display="Name" >
<Option selected="selected"></option>
</cfselect>

Edit: Well it seems Dan's Solution works in the sense of how I asked the question. I forgot to mention that I have 3 other form objects where I want their form values retained upon form submit. For example I have a textbox, a 2 radio buttons, and a checkbox that I want to retain their values. The values are obtained through a Bind that changes the value of these objects when I click a different object in the <Select>.

For example: <cfinput name="Chocolate" type="checkbox" bind="cfc:Candyland.getFat({CandyLand@click})" bindAttribute="checked" value="#Form.Chocolate#">

Upon submit despite retain the clicked item, all objects go blank.

Était-ce utile?

La solution 2

cfselect has a selected attribute. You just have to check to see if the form has been submitted.

if (StructKeyExists(form, "CandyLand"))
SelectedValue = Form.CandyLand;
else
SelectedValue = GetCandyInfo.UserID[1];


<cfselect name="CandyLand" etc
selected = SelectedValue>

Autres conseils

You're making your life harder for yourself by using <cfselect>. Just use a normal <select>, then set the appropriate option to be 'selected' via checking its value with the form submission.

<select name="CandyLand" [etc]>
    <cfloop query="getCandyInfo">
        <option value="#userId#"<cfif userId eq form.CandyLand> selected</cfif>>#name#</option>
[etc]

There is an active move in the CFML community to ditch this kind of cruft from the language, and encouraging people to use more robust solutions:

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top