in reply to Perl-CGI refresh with different param(s)??

Hi,

When you fill out a form, and click submit, the form sends name/value pairs for each form input element to the url specified in the form's action attribute.

If the form specifies method="get", then the name/value pairs are tacked onto the end of the url specified in the form's action attribute. For instance:

http://www.some_site.com/dir1/page1.htm?name1=val1&name2=val2

The name value pairs are in this part:

name1=val1&name2=val2

Note that the name value pairs are separated by '&', and the whole series of name/value pairs is tacked onto the end of the url with a '?'.

If you want to provide links (= a tags) for your users with different name value pairs, then you can construct the url's with the desired name value pairs yourself. For instance:

use strict; use warnings; use 5.010; my $age = 10; my $name = 'Tom'; my $webpage = "http://www.somesite.com/page1.htm"; my $url = "$webpage?age=$age&name=$name";

The CGI.pm module allows you to use print() to send the html to the browser that you want the browser to display. So to display a link with the above url, you would do this:

my $html =<<"END_OF_HTML"; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Title of the document</title> </head> <body> <div>Here is a link for you to search for blah blah blah:</div> <a href=$url>link1</a> </body> </html> END_OF_HTML print $html;

Note that CGI.pm has some shortcut functions to print the doctype( header() ); the html, head, and opening body tag( start_html() ); etc.

Replies are listed 'Best First'.
Re^2: Perl-CGI refresh with different param(s)??
by jonc (Beadle) on Jun 20, 2011 at 21:03 UTC

    This is great stuff, really cleared it up.

    I think I'm close now. My one question is how to set the "value" in the name value pairs to the equivalent of making the param 'on'?

    I've done this:

    my $webpage = "http://myurl/cgi-bin/search2011.cgi"; my $url = "$webpage?subject=$c_subject; #From earlier declaration alre +ady posted print qq(<A HREF="$url">Subject</A>\n)."<br><br>";

    However, since the radio button wasn't clicked, this just a returns an empty search

    Should I use this type of thing:

    $query->param('foo','an','array','of','values'); This sets the value for the named parameter 'foo' to an array of value +s. This is one way to change the value of a field AFTER the script ha +s been invoked once before. (Another way is with the -override parame +ter accepted by all methods that generate form elements.)
      When a radio button is checked, the name/value pair for that specific radio button is sent to the cgi script. If a radio button is not checked, then nothing gets sent to the server for that specific radio button. So if you have nine radio buttons and the user checks the second radio button, then the second radio button's name/value pair will be sent to your cgi script. If you want to change that to make it appear that the user selected the eighth radio button, then remove the name/value pair for the second radio button from the url, and replace it with the name/value pair of the eighth radio button.

        This sounds exactly like what I need to do... But say when the output comes, the URL doesn't have the name-value pairs appended to the end? Are they hidden there (maybe GET wasn't chosen)? How can I remove them in this case?

        Thanks a lot, I guess one troublesome aspect is I am not in charge of the HTML stuff, so I'm working only on the CGI script.

        Perhaps a combo of fetching the parameters and delete() will work?

      You may not be in charge of the html, but you have to have a copy of the form, so you can see what you are working with.
        Thanks a lot, I can use your approach (shown at the stackexchange website). I changed the source to method "GET" to see the tags, then used those and changed it back to "POST". And it worked!