Forum OpenACS Development: problem with form generation

Collapse
Posted by yo ha on
Hi im relatively new to openacs and the tcl language. I have looked at sample examples in generating forms, but when i tried to create my own it gave me this error:

can't read "conn": no such variable
while executing
"ns_conn form $conn"
invoked from within
"set v [ns_conn form $conn]"

I have the form in one page and im trying to access the form variables in the other page. I have set the form action to the page trying to access the form variables.

This was my piece of code to try to access the form variable "surname":

< %
set formvar [ns_conn form $conn]
set surname [ns_set get $formvar surname]
% >

Thanks for your help.

Collapse
Posted by yo ha on
Actually i have mistyped the piece of code trying to access the form variables. It is actually:

< %
set v [ns_conn form $conn]
set surname [ns_set get $v surname]
% >

Thanks.

Collapse
Posted by Brian Fenton on
Hello yo,

well, to answer your question, the reason you're getting the "no such variable" error, is because AOLserver is expecting a variable named "conn" and it can't find it. Normally you would create this variable in your tcl program by saying something like:
set conn "some value"

However, I don't think that's really going to solve your problem as I can see you are using the "ns_conn form" command and (as far as I know) this doesn't take any parameters (see the documentation on http://www.panoptic.com/wiki/aolserver/192)

What is it you are trying to do? I normally never use ns_conn because OpenACS has plenty of higher level functions for accessing the form variables (e.g ad_page_contract or template::form and also ad_form). If I were you I would search for existing examples of those ways of programming and then copy them.

hope this helps
Brian

Collapse
Posted by yo ha on
Hi Brian,

Thanks for your response. I am now able to read the form variables and ready for their processing. I have still stuck with the ns_conn way for simplicity. I am now going to read the other ways you have mentioned to do this.

Thanks,

Yoshi.

Collapse
Posted by Christian Eva on
Hi Yoshi

Here is a little proc that reads all the form vars and returns them as a list. you can use it like that if you want them in an array:

array set Formvars [z_as_getform]

# ==========================================================
# z_as_getform -- get form parameters
#
# USAGE:  z_as_getform  [-tolower]
#
# RETURN: tuple list of form values
# ==========================================================


proc z_as_getform {args} {
    global Page
    
    set L {}
    set f [ns_conn form]
    if ![string length $f] {
        return $L
    }
    set fsize [ns_set size $f]
    
    set i 0
    while { $i < $fsize } {
        set key [ns_set key $f $i]
        set val [ns_set value $f $i]
        set lkey [string tolower $key]
        if [string match -tolower $args] {
            lappend L  $lkey $val
        } else {
            lappend L  $key $val
        }
        incr i
    }
    
    return $L
}