0

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?

noah
  • 174
  • 9
  • Nothing to do with your question, but it is good to get into the habit of scoping your variables. Instead of writing `#listPersons#` in chosen.cfm, use `#FORM.listPersons#` as in [Matt's example](http://stackoverflow.com/a/23611589/104223). – Leigh May 13 '14 at 13:19

3 Answers3

4

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>
Matt Busche
  • 14,216
  • 5
  • 36
  • 61
  • 2
    The closing `` tag isn't mandatory: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option – duncan May 12 '14 at 14:44
  • @duncan that makes sense. I figured out his actual error though and have updated my answer to reflect that (although my original solution did fix his problem, I explained it wrong) – Matt Busche May 12 '14 at 15:21
  • Even easier would be to use the query, value and display attributes of cfselect. – Dan Bracuk May 12 '14 at 16:16
  • Thanks a lot, now it works..! First I had a `cfoutput` but then the result was like this: `Person 1 Person 2 Person 3
    Person 2 Person 3
    Person 3` and if I use a `form` instead of a `cfform` i get an error..
    – noah May 13 '14 at 10:06
  • 1
    *i get an error* That suggests you are still using some CF specific form elements (`cfselect`, `cfinput`, ...) which *must* be nested inside a `cfform`. Replace them with the html equivalent (`select`, `input`, ...) and it should work fine with a regular `form`. – Leigh May 13 '14 at 13:14
  • okey thanks now I see.. and if I use normal html tags there aren't any disadvantages? – noah May 15 '14 at 10:38
1

Your code works for me with my test database but the value of listPersons on Chosen.cfm is not what I think you intended it to be. I would change the code to the following:

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

What I did is I changed your CFLOOP to a CFOUTPUT then removed the CFOUTPUT you had. I also added the HTMLEditFormat functions just in case NAME or VORNAME contain some characters that will not play nice with the display. I assumed ID is numeric and GEBDATE is a date so figured no need on those. I also removed the BR element from your OPTION, not that I thought it was causing an issue but I could not see how that would effect the display either so seemed unneeded. I'd personally would close the OPTION but it is not needed to run. If you ultimate code is not running anything that CFFORM offers then I'd not use it and just use an HTML FORM.

Then on Chosen.cfm I would scope the output:

<cfoutput>#Form.listPersons#</cfoutput>
Snipe656
  • 845
  • 7
  • 15
1
<cfoutput query="getAll">
#id# #name# 
</cfoutput>

You don't need to repeat the query name inside of a cfoutput loop, if with cfoutput you specify the query you are looping over.

jaywebguy
  • 43
  • 6