Copy link to clipboard
Copied
I have 2 tables:
1. cities table (contains all cities in Nevada)
2. forms table (contains all forms that has an idcity field and refers to idcities in the cities table)
I run a CFSelect query to pull the list of cities from cities table
"<cfselect name="idcity" query="qCity" value="idcities" display="name" queryposition="below"><option></option></cfselect>"
So this list all the names of the cities in the drop-down and I can select and insert/update the idcities value (id number from cities table) into the idcity field (forms table).
However, when the page reloads, the CFSelect is not automatically selecting the previous selection (idcity field number). It automatically selects the blank <option>. I inspect the database and the idnumber was successfully inserted in the idcity field (forms table)
I think I need to run another query as "qForms" to fetch this data properly when the page reloads. but is it possible to have 2 queries (1 fetching the list of cities in the cities table to initiate the list and another query fetching the idcity field in the forms table to automatically select the data) in 1 cfselect?
I don't know what to do. Please help
Copy link to clipboard
Copied
One way to preserve form field selections is to write them to a cookie when the form is submitted and read the cookie when the form page loads.
No matter what method you choose, you'll have to do some if/else logic to see if the previous value exists.
Copy link to clipboard
Copied
I finally figured it out! I spent countless hours trying different settings/code to make this work and it's jus ta simple "selected" attribute calling the idcity field from the forms table:
"<cfselect name="idcity" query="qCity" value="idcities" display="name" selected="#qForms.idcity#" queryposition="below"><option></option></cfselect>"
Summary:
I name my cfselect "idcity" based on the field in my forms where I want my values to be inserted/modified to.
I run a query "qCity" calling the idcities and name for each city in the cities table to be as the list in my cfselect.
I added a "selected" attribute so when I run the page/reload after modify, I can call "#qForms.idcitiy" which is an idcity from my forms table as qForms query.
Now this automatically selects the item based on the idcity value from my forms table.
I really want to stay away on writing if statement if I can to avoid multiple lines of code in my page. So I guess I answered my own question. hehehe..
Thanks for viewing and for the answer!
Happy Coding!