Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to recycle some code previously used.
$upload_dir = "/home/upload"; $query = new CGI; $filename = $query->param("data"); $filename = $query->param("data2"); $filename =~ s/.*[\/\\](.*)/$1/; $upload_filehandle = $query->upload("data"); $upload_filehandle = $query->upload("data2"); #read and upload the file open UPLOADFILE, ">$upload_dir/$filename"; etc
The param(data) was used previously in another link and here i am interested in uploading data2 (this works fine). However, the form submited should call a different script dependent on the $filename. if $filename was data then the link would for example www.processdata1.com if $filename was data2 then link would be www.processdata2.com.
<FORM ACTION="http://www.xxx" METHOD="post" ENCTYPE="multipart/form-data"> File to upload: <INPUT TYPE="file" NAME="data2"> <INPUT TYPE="submit" NAME="Submit" VALUE="Upload"> </FORM>
rather than having to separate forms how can i achieve this? Thanks

Replies are listed 'Best First'.
Re: form cgi
by cbrandtbuffalo (Deacon) on Jul 07, 2005 at 16:25 UTC
    To clarify, do you want to call different scripts depending on data submitted as part of the form? So if they give you data, call this script, if they give you data2, call this other script?

    The 'action' in your form tag determines what will be called, but you can't dynamically change the action (well, maybe with Javascript). But you could create two forms with two different actions on the same page. You could also have both call a single script and have that script be smart enough to dispatch requests to one script or another on the server.

Re: form cgi
by jhourcle (Prior) on Jul 07, 2005 at 17:20 UTC

    Building on what cbrandtbuffalo said ...

    I'd probably either seperate this into two seperate forms (if the user can figure out which input to use, they can figure out which form to use)

    JavaScript is a possibility, but then you run the risk of what to do if the user has it turned off.

    Ideally, you could just send a redirect (302) to the other system, but because some browsers handle 302 as a 303 (doing a GET on the location you're trying to redirect to, rather than sending the POST again), this isn't reliable, either.

    Your last possibility is having script1 proxy the connection to script2, but I personally don't think this is a particularly good move in the long run (unnecessary load on server1, and makes script2 dependant on someone not messing with script1 and server1)

      thanks for thos replies
Re: form cgi
by ww (Archbishop) on Jul 07, 2005 at 16:50 UTC
    Simply put, you can't make the scenario you outlined work as you expressed it. See cbrandtbuffalo's reply above for one reason why.

    Here's another (also with caveat that you might be able to do something (extremely risky) with jscript): the
    <INPUT TYPE="file" NAME="data2">
    determines what your (server-side) script expects AND uses.

    And are you really hopeing to upload to TWO different sites, "processdata1.com" and "processdata2.com" from a single script? To me, that suggests MASSIVE security gaps, among other, more mundane problems.

    Using two forms can be tricky and can certainly provide more opportunities for user_errors. Nonetheless, it may still be more nearly foolproof than asking users to (correctly) provide a filename in a (text) input or via radiobutton or whatever, and then matching that (possibly garbled) data against what you think you want processed by which script...after which you'll still have to untaint the data in the file before you let it get anywhere near the system.

    Update ...to say that I will try to make last para easier to read as soon as I relocate/reconnect language centers in brain. Sorry. That's almost as hard to grok as OP's statement of problem

Re: form cgi
by splinky (Hermit) on Jul 07, 2005 at 16:14 UTC
    If you look in the documentation for the CGI module, you'll find a section titled, "OBTAINING THE SCRIPT’S URL". I believe the answer to your question lies there.

    Update: I misread the question. Never mind.