Skip to main content
Inspiring
Sep 1, 2017 10:54 PM UTC
Answered

Cant get "selected" to work when looping through months and dates

I have this code that outputs select lists of months and select lists of the dates (1-31)

The form loads OK and the months and dates are in the lists just fine.

But when I select a month and a date I want it to stay selected when I submit the form

So if I choose May 5 and click submit

I want it to look like this

But no matter what I select it always ends up January and 1

Here is the code

<tr>

            <th>Choose  Feed Date:</th>

            <th align="left"> <select name="feed_date_m">

                <cfloop index="m" from=1 to=12>

                  <option value="#m#" <cfif #form.feed_date_m# is "m">selected</cfif>>#monthasstring(m)#  </option>

                </cfloop>

              </select>

              <select name="feed_date_d">

                <cfloop index="d" from=1 to=31>

                  <option value="#d#">#d#  <cfif #feed_date_d# is "d">selected</cfif></option>

                </cfloop>

              </select></th>

          </tr>

This topic is closed to new replies. Start a new post to keep the conversation going.
haxtbhCorrect answer
Inspiring
Sep 4, 2017 7:39 AM UTC

By doing <cfif #form.feed_date_m# is "m"> you are saying "Is the value of form.feed_date_m equal to the character "m".

It is not checking the loop index value.

You want to do something like this:

<cfif form.feed_date_m EQ m>

(You dont need the # in cfif)

4 replies

haxtbhCorrect answer
Inspiring
Sep 4, 2017 7:39 AM UTC

By doing <cfif #form.feed_date_m# is "m"> you are saying "Is the value of form.feed_date_m equal to the character "m".

It is not checking the loop index value.

You want to do something like this:

<cfif form.feed_date_m EQ m>

(You dont need the # in cfif)

Translate
weezerboyAuthor
Inspiring
Sep 4, 2017 9:40 PM UTC

Some progress

THIS WORKS

<select name="feed_date_d">

                <cfloop index="d" from=1 to=31>

                  <option value="#d#" <cfif form.feed_date_d EQ d>selected</cfif>>#d#</option>

                </cfloop>

              </select>

THIS DOESNT??

        <select name="feed_date_m">

                <cfloop index="m" from=1 to=12>

                  <option value="#m#" <cfif form.feed_date_m EQ m>selected</cfif>>#monthasstring(m)</option>  <

                </cfloop>

              </select>

  Any idea why the DATE works but the month doesn't?

Translate
WolfShade
Legend
Sep 5, 2017 1:33 PM UTC

If this code you provided is copied and pasted from your codeset and not manually typed in, then you need to add a hashtag to the end of #monthasstring(m)

HTH,

^ _ ^

Translate