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

Hi dear friends
I have a cgi page written by perl my code is :

#!/usr/bin/perl -w use CGI; use warnings; $query = new CGI; print $query->header(); print $query->startform(action=>$site); print "<TABLE CELLPADING=0 BORDER=0 ><TR>"; print "<TH>cool1</TH><TH >col2 Site</TH><TH col3</TH><TH>col4</TH><TH +>col5</TH></TR>"; print "<TH>",$query->popup_menu('SRC_ADDR',['ADD1','ADD2'],default=>' +ADD1'),"</TH>"; # some other perl code # Now i want to change the action of form # if SRC_ADDR=ADD1 action of form should change to http://192.168.100 +.10/cgi/bin/addr1.cgi # if SRC_ADDR=ADD2 action of form should change to http://192.168.100 +.11/cgi/bin/addr2.cgi #Other perl code $temp=$temp+1; print "</TABLE>\n</DIV>\n"; print "<P>",$query->submit(); print $query->endform; print "<HR>\n";

How can i change the action of my form during the program thanks for your help

20040316 Edit by Corion: Added formatting, code tags

Replies are listed 'Best First'.
Re: changing form action
by tinita (Parson) on Mar 16, 2004 at 13:53 UTC
    by using the right parameter to the start_form()-method:
    print $query->startform(action=>$site, method => 'your desired method');
    see the documentation of CGI.pm for details.

    if you have printed the form-tag already, there's now way to change it (doesn't that sound logical?), since it has probably already made its way to the client.
    you have to go for another approach: don't print until the point where you want to change, but collect everything in a variable, then print it.

    update:
    of course i meant 'action' instead of 'method', i somehow read 'method'. but still it's the same thing and the same solution

Re: changing form action
by Happy-the-monk (Canon) on Mar 16, 2004 at 13:57 UTC

    How can i change the action of my form during the program

    You can make the for chose some values popup_menu to return to the script mentioned in the <form>-tag. You can't however, change the name of that script, as far as I know.

    That script can call a different script if you like. It could

    1. redirect the browser to a different URL location and maybe provide some of the values of the form to the remote CGI.
    2. use LWP::Simple to call the remote programme and work on its output.
    3. Sören

      when i use :
      $query->header(); # some other code print $query->redirect('http://127.0.0.2/cgi-bin/18.cgi?12');
      it returns: Status: 302 Moved Location: http://127.0.0.2/cgi-bin/18.cgi?12 and if i remark $query->header(); it gives : Internal Server Error
        You shouldn't specify a header when you use the redirect() method.

        The Internal Server Error may be coming from the correct place. What do you get when you go to the link (http://127.0.0.2/cgi-bin/18.cgi?12) using your browser?

        bassplayer

Re: changing form action
by Rich36 (Chaplain) on Mar 16, 2004 at 14:57 UTC

    If you're going to change the form action based upon values set in the form itself, it would be easy to do with JavaScript. You can change the action property of the form dynamically.

    From www.devguru.com:

    action Property
    This property specifies the URL address to which the data
    gathered by the form will be submitted. An email address
    can also be specified using the 'mailto:anybody@anywhere.com' syntax.
    
    Syntax: object.action = URL 
    

    «Rich36»