Francisco Costa
Francisco Costa

Reputation: 379

Populate a HTML SELECT using SQL and CF with a condition

I would like to populate 2 HTML elements SELECT using CF and SQL, the 1st select gets is options from all the elements in the TABLE Stakes, the 2nd SELECT gets is options from the TABLE Wards where the elements have the same StakeId as in the option selected in the 1st SELECT. I been trying to do this for hours, but i can't get to work the 2nd SELECT. Here is my code:

1st SELECT

<CFQUERY NAME="getStakes" DATASOURCE="#APPLICATION.ds#">
    SELECT * FROM Estacas
</CFQUERY>

<SELECT NAME="Stakes">
    <CFOUTPUT QUERY="getStakes">
        <OPTION VALUE="#estId#">#estName#</OPTION>
    </CFOUTPUT>
</SELECT>

2nd SELECT

<CFQUERY NAME="getWards" DATASOURCE="#APPLICATION.ds#">
    SELECT * FROM Alas WHERE estId = form.Stakes
</CFQUERY>

<SELECT NAME="Stakes">
    <CFOUTPUT QUERY="getWards">
        <OPTION VALUE="alaId">#alaName#</OPTION>
    </CFOUTPUT>
</SELECT>

Can anyone help me?

Upvotes: 1

Views: 1391

Answers (1)

Francisco Costa
Francisco Costa

Reputation: 379

I finally find out the best way to solve my problem using only cf and html, Scott Stroz was right, about using the group attribute but back then i had no clue what he was talking about, this way there is no need to use other languages, like javascript. Thanks Scott even though i just found out what you were talking about after knowing this lol.

<cfquery name="getAlasEstacas" datasource="#APPLICATION.ds#">
    SELECT a.alaId, a.alaName, e.estName 
    FROM alas a
    INNER JOIN estacas e ON e.estId = a.estId
    ORDER BY e.estName, a.alaName
</cfquery>
<select name="AlaId">
    <cfoutput query="getAlasEstacas" group="estName">
        <optgroup label="#estName#">
            <cfoutput>
                <option value="#alaId#">#alaName#</option>
            </cfoutput>
        </optgroup>
    </cfoutput>
</select>

Upvotes: 2

Related Questions