Forum OpenACS Q&A: ns_returnredirect FORM vars vs URL vars

I have used, and the toolkit uses ns_returnredirect with URL vars extensively:

ns_returnredirect elsewhere.tcl?[export_url_vars var1 var2 var3] 

I would like to instead use
ns_returnredirect with FORM vars
This will allow me conditionally pass information offsite to a shopping cart and in another case, a Real Audio host after verifying user status and perhaps stuffing the DB with stats.

Any suggestions?
TIA
-Bob

Collapse
Posted by Ola Hansson on
I doubt that this is what you're asking, but here goes:

It's legal for you to write:[export_url_vars var1 var2 var3 foo=$bar]

...maybe I'm lost here😉

Collapse
Posted by james shannon on
Hello...
  I know what you're asking, but it's not /easily/ done. Here are your options:

1) Although a lot of sites specifically request(and use in their examples) FORM, or POSTed variables, they will still accept and parse the URL, or GET variables. Of course, if the information you're sending is not suitable for for URL variables then you will have to continue...

2) Have an interim page on your server that takes URL variables and has a few tasks. First, it does all the db stuffing, etc. Then, it creates a simple HTML page and creates some quick javascript that will redirect the user with the necessary POST data.

3) Otherwise, you'll have to make the request yourself(look for util_http* in the tcl directory) and either send the results back to the user, or hope that you get some sort of sessionid back that you can then give to the user as you pass her off to the third party site.

it all depends on your exact requirements

Collapse
Posted by Bob OConnor on

Hi James, I LIKE your #2:

"Have an interim page on your server that takes URL variables and has a few tasks.....Then, it creates a simple HTML page and creates some quick javascript that will redirect the user with the necessary POST data."

This simple HTML page:

  • Does the user see it, or does it just flash on the screen or not appear at all (Preferred)?
  • Can you give me a javascript code fragment example that does this?

Thank you.
-Bob

Collapse
Posted by james shannon on
it would be an immediate redirection. i don't have any code, but i've seen it done :)....

all i can say is look around on websites that use java to make POSTs to other websites... hopefully somebody on here can either supply the code or point you in the right direction...

if you are still unsuccessful, i'll take a look

Collapse
Posted by Chris Rasch on
You may wish to check out Rob Mayoff's utility dqd_internalredirect - a wrapper around the C API function Ns_ConnRedirect. This lets you send back the contents of a different URL (from the same server) without letting the client know. See http://dqd.com/~mayoff/aolserver/ for details. It doesn't solve the problem you describe, but perhaps you may find use for it in another context.
Collapse
Posted by Robert Ezman on
I'm guessing that what you want to do is process form vars from a previous page then decide what you want to do with them and redirect accordingly.

That's no problem...Imagine that your previous page has a form with var "formvar1"...in your redirect/processing page you'll do the following:

set formvar1 [ns_queryget "formvar1"]

#now of course you could use the ad utils for setting vars

#now do your processing

if { option1 } {

  ns_returnredirect "nextpage.adp?fromvar1=$formvar1&othervar1=$othervar"

#note the above is supposed to be one line

} else {

ns_returnredirect "otherpage.adp?formvar1=$formvar1"
}

If you want to create a link that passes stuff along:

<a href="nextpage.adp?formvar1=formvar">Add to cart</a>

#don't forget to escape the quotes

If you want to pass stuff from a previous form into a new form

<form ...>
<input type=hidden name=formvar1 value="$formvar1">
...
</form>

So you have three choices:

1) automatically redirect if you don't need user input...

2) pass info along in another form if you can have the user push a button

3) encode the form info into a link and have the user click on the link

Hope this helps.

Collapse
Posted by Bob OConnor on
p>Hi Robert,

You wrote:
"So you have three choices:
1) automatically redirect if you don't need user input...
2) pass info along in another form if you can have the user push a button
3) encode the form info into a link and have the user click on the link
"

Actually I like "Option D" best... or is it 4 😊

I want to:

  • Get a bunch of form vars from USER
  • Do Processing and conditionals.
  • Send bunches of form vars (NOT url vars) to an off site, without any further user intervention.

So far, what I have come up with is to put this javascript code on my Processing page.

(FORM NAME="myForm")
...
(/FORM)

(SCRIPT LANGUAGE="JavaScript")(!--
setTimeout('document.myForm.submit()',50);
//--)(/SCRIPT)

I haven't tried it yet but it looks promising and if I get it working, I'll post results to this thread. Thank you.

PS: The Javascript tags above seemed to cause a 500 error when I SUBMITted it to this forum so I put and "( )" instead of < > in the code above to prevent the error.

-Bob

Collapse
Posted by Robert Ezman on
This is where you start getting into a personal taste/philosophy thing.

One thing you may want to take in to consideration is that as soon as you start using javascript the chance of the browser crashing or some other anomoly that will mess you up happening....like for example the surfer turns javascript off...and then what...

I personally try to keep it simple: pure html...

What is the difference between a form var being passed and a url var.... The only difference that I see is that with the url you actually see the parameter on the location bar....Note by the way that if you change you method in the form definition from post to get...all the sudden it gets passed along as a url var..

I guess the only problem is what's on the other end of the request...I'm guessing that the info is not being passed to an aolserver web server.... because aolserver doesn't seem to care....the vars still get decoded properly.  What I mean is that if you have two pages: one request with a form and one that does the action and decodes the values passed.  If you're using adp pages and ns_queryget if you change the method= to get from post or vice versa you application will still work.

Collapse
Posted by Bob OConnor on

Well, Great Advise Robert.

I wasted a bit of time getting the javascript above to work. It was flaky and sometimes didn't pass the Form Vars. ? So I changed tack:

I changed the Form Method from POST to GET so I could recover the long and complicated URL… the longest of which was 241 characters. Then I just used:

ns_returnredirect /the/very/long.tcl?character+string=241&characters=

And it works. It's a Kinda messy URL but hopefully users won't notice or care.

Maybe later, for extra credit I could develop an ns_returnredirect that sends data as POST...

-Bob

Collapse
Posted by Robert Ezman on
If you want to obscure some of the data in the location bar...Particularly if it's private info.... what you can do is base64 encode it (or use another kind of encoding).  The reason I suggest base64 is that that there's a function for it in the ad toolkit.

So you would do the following:

bulkmail_base64_encode string_to_obscure

and on the other end

bulkmail_base64_decode string_to_obscure

the result is uninteligible (to the user) junk in the location bar

This is just an idea...I haven't actually tried it as I've never had to obscure my info passed...however I've kept it as a plan b just in case I'd ever need to.

BTW:  A ns_returnredirect that sends data as POST would be great....If you do actually do it (or anyone else for that matter) it would be great if you could post it.

Collapse
Posted by Andrew Piskorski on
If you must accomplish a redirect with POST rather than GET, then JavaScript is the only known way to do it.

Say normally submit a form to page B, but need to give the user the option to submit it to page C instead. Here's a code snippet which creates a URL which when clicked, will submit a form to the current page, rather than whereever the form's action property would normally send it:

set submit_form_to_self_link "
    <a href="javascript:submit_form_to_self()">re-calculate</a>
    <script language=javascript>
    function submit_form_to_self() {
         document.[export_var name_of_form].action = '[ns_conn url]?[ns_conn query]';
         document.[export_var name_of_form].submit();
    }
    </script>
    <noscript></noscript>
"

You'll want to avoid doing things like using ns_returnredirect to redirect to a page that then does something and uses JavaScript to automatically submit a form, as the user will sit there staring at the page for several seconds before the auto-submit finally takes effect. But for user-initiated form submissions (clicking on a link or form button), it is fine.

(Actually, you'll want to avoid ever doing more than one redirect in a row, or redirecting with a very long URL, as Internet Explorer will often refuse to follow it - and for IE 5.x, its error message will say that your site is down, not that IE refused to follow the redirect.)