Forum OpenACS Q&A: Multiple User Inputs

Collapse
Posted by Roger To on
Hi,

i've got a problem with figuring out how to accept multiple inputs
from the adp page and passing those values to be processed
individually in a corresponding .tcl page.

What i've done so far is dynamically create several fields where users
can input information to be stored into a table. It currently looks
like this

for {set i 1} {$i <= $num_of_fields} {incr i} {

    append fields "

<input size=50 name=foo value='foo'>

<input size=50 name=foo_desc.$i value=@foo_desc.$i@>

"

where num_of_fields is the number the user sets which will dynamically
create the number of fields required for data entry. I have set foo to
be an array but i am not sure how to properly setup and utilise this
array(multirow?) and passing it to the tcl file to process each value.

The problem i have is trying to assign each field a unique variable
name and pass them through to another tcl file which will process
these inputs.
I don't think whatever i've done is at all correct. Could someone
direct me to the steps i should take in order to:
1) get a dynamic number of values from an adp page
2) pass these values to another tcl file for processing

Thanks

Collapse
Posted by Jeff Davis on
Here is a pretty simple example
in first.tcl: 
set x [list a b c d]

in first.adp: 
<master>
<form action="next">
<list name="x">
<input type="text" name="var.@x:rownum@" value="@x:item@">
</list>
<input type="submit">
</form>

in next.tcl : 

ad_page_contract {
  next page
} {
   var:array
}
doc_return 200 text/plain [array get var]
Collapse
Posted by Roger To on
Just a couple of questions regarding the code

1) does that mean that at most i can store only 4 variables?? what happens if a user decides they need 20 inputs for example?

2) what does x:rownum and x:item represent?

3) does var.@x:rownum@ create a variable in array called x:rownum? (although i don't know what x:rownum represents)

Collapse
Posted by Roger To on
i also got a request error when i tried

set x list[a b c d]

i'm a newbie to this, so i'm not quite sure what's goin on with all of this

Collapse
Posted by Jeff Davis on
Roger, you do as many variables as you want (I stopped at 4 since
I get tired of typing).

x:rownum is the index of the element of the list and x:item is the
value of the element.  the list tag just iterates over the elements
of the named list...

var.@x:rownum@ is not an array, it is just the name of the variable passed
to the next form.  The actual array is created by ad_page_contract.

You should read /doc/acs-templating/ and /api-doc/proc-view?proc=ad_page_contract
on your local install (or at dev.arsdigita.com if you local install
is broken for some reason).

Oh, and you did not type what I sent...
it is:

set x [list a b c d]

(the list command after the [)

Collapse
Posted by Jun Yamog on
Hi Roger,

I believe you can also have the option of using multiple of ad_page_contract.  Try to search ad_page_contract on http://youropenacs/api-doc

That way you will just name your var the same.  And on the next tcl file parse them out.  Sorry kinda very tired and sleepy.  Try to grep around for examples.  I think this is the proper way to do it.

In http it is valid to have 2 or more values to a single variable.  I hope this helps.

Collapse
Posted by Jeff Davis on
Jun, there is not a right or wrong way to do it, you use multiple
for some things and array for others.  If it is a form to edit a field
for a bunch of different object_ids you would use array and have
the key be the object id.  One issue with multiple is
if order is important you probably should not use it.
I don't think the get/post has to present url vars in the same order as the form.

For example for 3 lines of address input each in a seperate
input field, if they were not identified it is possible you could get them back in a different order.

Maybe the spec says the client must
return variables in the order they are encountered in the form
but I don't remember ever seeing anything like that and I would
hate to have to count on it.

Collapse
Posted by Jun Yamog on
Hi Jeff,

Sorry about that.  Did not mean that my post was right and yours was wrong.  I meant it only as another option.  I guess I was too tired to review my post, been coding in CCM for 15 hours straight.