Re: [AOLSERVER] Form variables

2001-05-15 Thread Allanah Myles

This is what I do (or at least, something vey similar to it):

proc fetch_form_variables {varName} {
upvar 1 $varName arrayName
set form [ns_getform]
set size [ns_set size $form]
for {set i 0} {$i < $size} {incr i} {
set [set arrayName]([ns_set key $form $i]) [ns_set value $form $i]
}
}

This proc takes the name of an array into which the key=value pairs
submitted should be set.  I don't do (and probably should) any error
checking to ensure that the name being passed is an array that already
exists (and perhaps we should throw an error and not clobber it).

Call it within code like:

  fetch_form_variables form

and access form variables like this:

  ns_puts "Hello, $form(username)!"


This brings me to ask the question:  why isn't there an [ns_set array]
which returns the ns_set as an array that can be passed to
[array set]?  Or, am I missing something?

- Dossy


On 2001.05.15, Eduards Cauna <[EMAIL PROTECTED]> wrote:
> What is the best way to access form variables (i.e., variables submitted
> using html form) in adp pages?
> There is code I am using now but not sure I am understand everything what
> there happens.
>
>
> proc fetch_variables { } { # fetch all variables from forms and
> fancy urls
> uplevel {
> set form [ns_getform]
> set form_size [ns_set size $form]
> set form_counter_i 0
> while {$form_counter_i<$form_size} {
> set [ns_set key $form $form_counter_i] [ns_set value $form
> $form_counter_i]
> incr form_counter_i
> }
>  }
> }

--
Dossy Shiobara   mail: [EMAIL PROTECTED]
Panoptic Computer Network web: http://www.panoptic.com/



Re: [AOLSERVER] Form variables

2001-05-15 Thread Rob Mayoff

> What is the best way to access form variables (i.e., variables submitted
> using html form) in adp pages?
> There is code I am using now but not sure I am understand everything what
> there happens.

The code you're using now has the bad effect of letting the client
pollute your Tcl namespace. Consider what will happen if I send you a
URL like "/foo/bar.tcl?::env(PATH)=/tmp".  You are much better off
putting the form variables into a Tcl array.

In my own code, I do it like this:

array set q {
someVariable defaultForSomeVariable
anotherVar anotherDefaultValue
}

getform q

And getform is defined like this:

proc getform {arrayName} {
upvar $arrayName a

array set a {}

set form [ns_getform]
if {[string length $form] == 0} return
for {set i 0; set n [ns_set size $form]} {$i < $n} {incr i} {
set key [ns_set key $form $i]
set value [ns_set value $form $i]
if {![info exists state($key)]} {
set state($key) 1
set a($key) $value
} elseif {$state($key) == 1} {
set a($key) [list $a($key) $value]
set state($key) 2
} else {
lappend a($key) $value
}
}
}

My definition of getform is a bit more complex than some others.  If the
form variable name "x" appears twice in the HTTP request, my getform
will set q(x) to a proper two-element Tcl list.  This is useful if you
have two INPUT TYPE=TEXT boxes with the same name.



Re: [AOLSERVER] Form variables

2001-05-15 Thread Michael Richman

In a message dated 5/15/01 11:11:32 AM Eastern Daylight Time, 
[EMAIL PROTECTED] writes:


> This is what I do (or at least, something vey similar to it):
> 
> proc fetch_form_variables {varName} {
> upvar 1 $varName arrayName
> set form [ns_getform]
> set size [ns_set size $form]
> for {set i 0} {$i < $size} {incr i} {
> set [set arrayName]([ns_set key $form $i]) [ns_set value $form $i]
> }
> }
> 


Two issues that I see with this code 

1) if i'm not mistaken, it will throw the error "can't read "arrayName": no 
such variable", because of [set arrayName] which is unneccessary.

2) if the form has values from a multiple select, array set -- if it was 
working -- would step on the duplicate keys each time, resulting in only the 
last value in the multiple list being set.

Rewritten, with multi-select support, it'd be:

proc fetch_form_variables {varName} {
    upvar $varName arrayName
    set form [ns_getform]
    for {set i 0} {$i < [ns_set size $form]} {incr i} {
        lappend arrayName([ns_set key $form $i]) [ns_set value $form $i]
    }
}

The problem with this is that if any of the elements you've grabbed are 
SINGLE ITEMS but MULTIPLE WORDS, you'll have a list element: 
   
   {i was a form input}

instead of 

   i was a form input

...which can cause issues.

(Compare these two lines of code:

   lappend foo "hello bar"
   set foo "hello bar"

Very different result.)

To fix, you could then do something like 

   if {[llength $arrayName($key)] == 1} {
   set arrayName($key) [lindex $arrayName($key) 0]
   }


N.B. upvar defaults to 1, so no need, really, to include the "1".

-- miichael

___
 michael richman 
 aol local technology 
 214.954.6204



Re: [AOLSERVER] Form variables

2001-05-15 Thread Rob Mayoff

+-- On May 15, Michael Richman said:
> N.B. upvar defaults to 1, so no need, really, to include the "1".

Unless some joker calls "fetch_form_variables 2".  Tcl has no problem
using numbers as variable names.