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

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.)