Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
We don't bite newbies here... much
 
PerlMonks  

onSubmit Redirect

by hok_si_la (Curate)
on Feb 04, 2005 at 16:47 UTC ( #428123=perlquestion: print w/ replies, xml ) Need Help??
hok_si_la has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I have a script that generates a form using CGI.pm. This script is kind of a multi-purpose script, so the form action varies. On one of the actions, I am trying to do something like this.....

$action = "/NewServer/scripts/admin/changestatus.pl NAME=form METHOD=p +ost onsubmit=window.location.href='getadminrequests.pl?$GRPATHCLEAN&a +rpath=$ARPATH&grpath=$GRPATH&requesttab=Server Request $id'\;"; <FORM ACTION=$action>

I would like the background script, 'changestatus.pl' in this case, to run while the browser is redirected to the ridiculous URL listed above. I read a few archived nodes about CGI::redirect but was unable to come up with as working solution. I would prefer to use CGI::redirect but using <script>window.location.href='ridiculous url'</script>is not out of the question.

Any pointers or references to related tutorials would be appreciated.

Thanks,
hok_si_la

Comment on onSubmit Redirect
Download Code
Re: onSubmit Redirect
by dorward (Curate) on Feb 04, 2005 at 17:46 UTC

    I'm a little unclear as to what you are trying to achieve, but it looks like you want to submit the form data to two seperate scripts, only one of which returns something useful to the user.

    Forget about "onsubmit", using client side scripting is unreliable at the best of times, and I don't think it can solve your problem anyway. The standard (and the only really safe) technique to do this is to:

    1. Submit the form to one script - specified in the action attribute of the form
    2. Have that script return a 302 (meaning Found) status in its http header along with a Location header that redirects the user agent to the next script in sequence. As you are working with a GET request you will have to build the URL to the second script programatically so you can include the submitted data.
    3. Have the second script return a suitable http header and the HTML document.

    If you need to decide which script to process the data with depending on a user selection in the form (including the clicking of a specific submit button) you can check the value of the parameter in the first script and use that to decide which (if any) other script to redirect to (i.e. what to specify after Location in the headers).

    (As a side note, when writing HTML (not HTTP headers) ampersand characters (&) must be escaped as (&amp;) except under certain circumstances, which the above data doesn't fall under, when the escaping is optional)

      First of all I thank both of you for your responses. Sorry I am just now responding. I was at lunch.

      I am passing all of the form element data to the script changestatus.pl. That script will open up a DBI session and update all the table columns. The URL literally contains about a paragraph of information. Real long story there. I originally had this at the bottom of changestatus.pl

      ########################################################### sub printDone ############################################################ { my $url = "http://nettools/NewServer/scripts/adminrequests.pl"; print "Location: $url\n\n"; }

      That worked out fine. The problem is that I now want the user to be redirected to a script (getlist) that lists all of the updated information. This script uses a tab function that I wrote which is why it needs cgi passed to it that would make any of Tolkien's works look like Cliff Notes.

      I have considerred three methods of doing this.
      1)Just use the code above but figure a way of going back 2 listings in the browser history.
      2)Pass all of the necessary variables (those in $action) to changestatus.pl and make the changes to the function above.
      3)Run a script while redirecting to the complicated URL I gave earlier. (Hardcoded equivelent to a history.go(-2)).This would be done using print redirect or something similar when the submit button is clicked.

      I hope that clarifies the situation. Essentially it would be best if I could figure out a way to accomplish method 3, but if not, I can find a workaround using one of the first two ideas.

      Thanks again,
      Shane
Re: onSubmit Redirect
by nobull (Friar) on Feb 04, 2005 at 19:21 UTC
    There may be a client-side JavaScript solution along the lines you describe but that has nothing whatever to do with Perl so I won't discuss that here.

    If you want a CGI script to return a response via the web server and also continue doing stuff in the backgroud you can just fork off and die. I should also point out that this is a generic CGI issue and also has nothing to do with Perl.

    So what you need to do is have your CGI script call fork(). The parent will then print the redirect (using CGI::redirect) and exit.

    The child will close(STDOUT);close(STDERR); (or if you are feeing more sane perhaps re-open STDERR to a log file somewhere).

Re: onSubmit Redirect
by FitTrend (Pilgrim) on Feb 04, 2005 at 19:36 UTC

    Here is what I would suggest:

    1. make a 0 size frame on the webpage (basically a hidden frame) to re-direct these kind of requests.

    <html> <head> <frameset rows="0,*" border="0"> <frame src="URL TO NOTHING (about:blank)" name="hiddenFrame" noresiz +e scrolling=no> <frame src="/cgi-bin/mainScript.pl" name="mainFrame" noresize> </frameset> </head> </html>

    2. When the form button is clicked, process that request and then print the following output to the html page:

    # PROCESS HTML SUBMIT HERE if (button clicked) { # any code needed before redirect print qq { <script language=javascript> window.open('/cgi-bin/changestatus.pl','hiddenFrame','') window.open('/cgi-bin/mainScript.pl?otherVar1=x&otherVar2=y','main +Frame','') </script> </html> }; exit; }

    If the changestatus.pl needs to complete first, you may need to put a sleep in your code before displaying the results of the second window.open statement. Don't wait too long or you'll annoy your users :)

    There might be a consolidated way to do this, but this has worked in several occasions for me.

    Sincerely,
    Marc

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://428123]
Approved by NetWallah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-04-19 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls