Forum OpenACS Q&A: Using GET and POST variables

Collapse
Posted by César Clavería on
Hello,

I'm having a little problem with POST and URL (GET) variables in the same request. When submitting a form only the POST variables are being received by the "action" even if I specify a variable in the action's query string.

I guess this is better explained with a little example:

Lets say I have this form:

< form id='test' name='test' action='test-target?getvar=123' method='POST'>

< input type='text' name='postvar' id='postvar'>
< input type='submit' value='submit'>
</ form>

Here the action is defined as: 'test-target?getvar=123', with a URL variable clearly defined and I have a field with the name 'postvar', this form is being submitted with the method POST.

Now on the form's target I have:

ad_page_contract {
} {
{getvar "lost variable"}
{postvar "lost variable"}
}

set parameters [ns_getform]
set method [ad_conn method]
set urlv [ad_conn urlv]
set get_var_set [ns_set get $parameters "getvar"]
set post_var_set [ns_set get $parameters "postvar"]

#info about the method and URL used
ns_log notice "method = $method"
ns_log notice "urlv = $urlv"

#Info from the ns_getform.
ns_log notice "get_var_set = $get_var_set"
ns_log notice "post_var_set = $post_var_set"

#Info from the variables defined on the page contract
ns_log notice "getvar = $getvar"
ns_log notice "postvar = $postvar"

If I check my log I get:

Notice: method = POST
Notice: urlv = test-target
Notice: get_var_set = < -------
Notice: post_var_set = var from post
Notice: get_var = lost variable < -----
Notice: post_var = var from post

The value from the "getvar=123" is now missing.

-----------------------------------------------------

I have tried this using AOLServer 4.10 and 4.5, using OpenACS 5.3.

Is there a way to use both of this variables??

Thank you in advance for your help.

Collapse
Posted by César Clavería on
OK, after some test I got a solution :).
A fairly simple solution.

It looks like AOLServer ignores the parameters in the query string if the method used was POST, but even then it is possible to obtain the query string using.

set query [ns_conn query]

and then parse it with:

set get_variables_set [ns_parsequery $query]

and finally we can get the variables value using the resulting ns_set

set getvar [ns_set get $get_variables_set "getvar"]

:D

Collapse
Posted by Ryan Gallimore on
Even so, it is much easier (and recommended) to use ad_form to build a form with OpenACS.
Collapse
Posted by Gustaf Neumann on
Ryan, this problem is independent of whether or not ad_form is used. It is a question, of what is specified in the action of a form. Btw, the class ConnectionContext in xotcl-core distinguishes as well between form parameters and query parameters for exactly the same reasons. There are situations, where a query is parameterized (via URL), which transmits a form via POST.
Collapse
Posted by César Clavería on
Thanks, good to know some other alternatives, I guess I have to learn xotcl.

My main problem with this was that the information was being sent trough a flash object, the flash object sends some information as POST, but I needed some more parameters so I sent them using GET. Thats why I came across this problem.

Collapse
Posted by Don Baccus on
I just ran into a similar problem myself, and there's an easier way.

First, to just get the query variables for either GET or POST you can do this:

ad_page_contract -form [ns_parsequery [ns_conn query]] ...

And define your variables in traditional ad_page_contract style.

This works for the case I'm working on, because the POST data is actually being sent as content rather than a form variable, and I get the data with ns_conn content (the content actually shows up in the global _ns_form, but as the *key*, with the value blank, i.e. AOLserver is being confused).

However, using ns_set merge, you should be able to get both the querystring vars and the form vars like this:

ad_page_contract -form [ns_set merge [ns_getform] [ns_parsequery [ns_conn query]]] {

Here's the documentation on ns_set merge:

http://aolserver.com/man/4.0/tcl/ns_set.html

If you call this script with GET, _ns_form will already have your query vars in it, and ns_set merge won't touch them in _ns_form.

On the other hand, call it with POST, and the vars defined in the query string will be merged with the form vars already set in _ns_form, and off you go.

I haven't tried the merge case (since I don't need it), but it should work ...