Forum OpenACS Q&A: handling an ad_form values generated dynamically

I got this confusing problem, maybe someone can help me here:

first is construted a form based on the data that the DB should have:

db_foreach my_query { *SQL* } {
    ad_form -extend -name my_form -form {
        {fte${grade_level_id}:naturalnum
            {label $description}
            {html {size 5}}
            {value $fte}
        }
    }
}

you see that the name of each  var is: fte${grade_level_id}

then on the -on_submit:
I need to use each one of those variables in this way:

-on_submit {
    db_foreach my_query { *SQL* } {
        my_proc $grade_level_id fte${grade_level_id}
    }
}

but at:  fte${grade_level_id}
that gives me the name of the variable,  not its content! and what is needed is the content.

TIA.

Collapse
Posted by Jade Rubick on
This might be totally off base, but would

[set ft${grade_level_id}] work?

Yes!
Thanks Jade! ;o)
I remember that I had some task like that before in handling variable names, and solved it at that time, but now I didn't remember how until your post.

Thanks again!!

actually, that ad_form doesn't work, the variable ${grade_level_id} seems to not be available for it inside the db_foreach?

so I substituted with:
template::element create my_query fte${grade_level_id} \
        -datatype naturalnum -widget text -label $description -html {size 5} -value $fte

=)

Collapse
Posted by Dave Bauer on
The variables are not substituted at that level in ad_form You can build up the form definition as a list like this:
set form [list]
db_foreach my_query { *SQL* } {
    lappend form [list fte${grade_level_id}:naturalnum [list label $description] [list html [list size 5]] [list value $fte]]
}
Another trick you can use it to put the values into an array by using a . in the element name.
set form [list]
db_foreach my_query { *SQL* } {
    lappend form [list fte.${grade_level_id}:naturalnum [list label $description] [list html [list size 5]] [list value $fte]]
}
Then you can add fte:array,optional in ad_page_contract and an array named fte will be created with one element for each grade_level_id with the grade_level_id as the key.
Thanks Dave, you are right.

Also, I noticed that to get the value of a variable is also available:
template::element::get_value my_form fte${grade_level_id}