Skip to main content
Inspiring
September 1, 2017
Answered

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

  • September 1, 2017
  • 1 reply
  • 1390 views

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 has been closed for replies.
Correct answer haxtbh

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)

1 reply

haxtbhCorrect answer
Inspiring
September 4, 2017

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)

weezerboyAuthor
Inspiring
September 4, 2017

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?

WolfShade
Legend
September 5, 2017

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,

^ _ ^