Search code examples
coldfusioncfselect

ColdFusion not able to read option value of a cfselect


I'm a beginner with ColdFusion and I'm just trying out some basic functions.. I tried to loop over a simple query and put the values in a of a element. As value for the element I tried to set the id of each record of the query. After submiting I tried to read the selected value but I only get

You have chosen #getAll.id#

Here is my Code:

index.cfm

<cfquery datasource="testdb" name="getAll">
  select *
  from Personen
</cfquery>
<cfform action="chosen.cfm" method="post">
  <cfselect name="listPersons">
    <cfloop query="getAll">
      <option value="#getAll.id#"><cfoutput>#getAll.id# #getAll.name# #getAll.vorname# #getAll.gebdate# <BR></cfoutput>
    </cfloop>
  </cfselect>
  <cfinput type="Submit" name="Senden" value="Senden">
</cfform>

chosen.cfm

<cfoutput>You have chosen #listPersons#</cfoutput>

Can you tell me where I've made the mistake?


Solution

  • You didn't put your value attribute in a cfoutput tag, so it's being processed as #getAll.id# as the key in the struct instead of the value from the query. If you update your cfloop to be a cfoutput your issue will be fixed.

    A couple pointers - You should scope the variable on chosen.cfm and you don't need to use cfform a regular form works just fine.

    <cfquery datasource="testdb" name="getAll">
      select *
      from Personen
    </cfquery>
    <form action="chosen.cfm" method="post">
      <select name="listPersons">
        <cfoutput query="getAll">
          <option value="#getAll.id#">#getAll.id# #getAll.name# #getAll.vorname#     #getAll.gebdate#</option>
        </cfoutput>
      </select>
      <input type="Submit" name="Senden" value="Senden">
    </form>
    

    chosen.cfm

    <cfoutput>You have chosen #form.listPersons#</cfoutput>