Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Perl-CGI refresh with different param(s)??

by jonc (Beadle)
on Jun 20, 2011 at 19:37 UTC ( #910616=perlquestion: print w/ replies, xml ) Need Help??
jonc has asked for the wisdom of the Perl Monks concerning the following question:

Hello and thanks for your time,

I have a Perl script that is called when a user selects some "radio" buttons and enters a searchword in text. When the output is returned, I would like to provide the user some hyperlinks that will allow them to redo the search with different radio buttons pressed (without having to go back and do it manually). The same searchword (inputted 'query') is to be used.

Example Perl script:

my $search_key = param('query'); my $c_verb = param('verb_only'); my $c_enabling = param('enabling'); m +y $c_subject = param('subject'); my $c_object = param('object'); my $c_prep = param('prep'); my $c_adj += param('adjective'); my $c_modnoun = param('modnoun'); my $category_id; my @all_matches; ## RESULTS OF SEARCH if ($c_verb eq 'on') { if ($c_enabling eq 'on') {$category_id = 'xcomp';} elsif ($c_subject eq 'on') {$category_id = 'subj';} elsif ($c_object eq 'on') {$category_id = 'obj';} elsif ($c_prep eq 'on') {$category_id = 'prep';}

Say, the first time, $c_enabling was 'on'. I would like a hyperlink to a search where $c_subject is 'on' but $searchkey is the same.

I've read the CGI module on CPAN (url() or self_url), and a bit about POST and GET, but it is all new to me.

Thanks again, and let me know if this is unclear

Also, this has been extended(I'm sorry, I didn't know this was discouraged, I thought starting fresh would be better for a second opinion) at: http://stackoverflow.com/questions/6418292/how-do-i-substitute-unseen-name-value-pairs-from-radio-buttons-in-my-cgi-script

Comment on Perl-CGI refresh with different param(s)??
Download Code
Re: Perl-CGI refresh with different param(s)??
by 7stud (Deacon) on Jun 20, 2011 at 20:23 UTC

    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.

      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.
        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.
Re: Perl-CGI refresh with different param(s)??
by Your Mother (Canon) on Jun 20, 2011 at 20:31 UTC

    Probably you want self_url in CGI.

    CREATING A SELF-REFERENCING URL THAT PRESERVES STATE INFORMATION: $myself = self_url; print q(<a href="$myself">I'm talking to myself.</a>);

    It's best mixed with CGI->url (see docs) and URI/URI::QueryParam for robustness and ease of reading/maintenance.

Re: Perl-CGI refresh with different param(s)??
by 7stud (Deacon) on Jun 20, 2011 at 20:39 UTC
    self_url() just returns the url of the cgi script with the name/value pairs that the user entered into the form tacked onto the end of the url. So if self_url() returned this url:

    "http://www.somesite.com/page1.htm?age=10&name=Tom"

    You could do this:

    my $url = self_url(); $url =~ s/name=Tom/name=Betty/; print $url, "\n"; --output:-- http://www.somesite.com/page1.htm?age=10&name=Betty

    Then you could insert the altered url in the html output that you send back to the browser.

      Altering URIs with regular expressions can get weird and break pretty fast. Something like this, using the modules I recommended earlier, is better-

      perl -MCGI=self_url -le 'print self_url' "o=hai;i=can;haz=cheezeburger +" http://localhost?o=hai;i=can;haz=cheezeburger perl -MCGI=self_url -MURI -MURI::QueryParam -le \ '$u = URI->new(self_url); $u->query_param(haz => "tacoz"); print $u' + \ "o=hai;i=can;haz=cheezeburger" http://localhost?o=hai;i=can;haz=tacoz

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (11)
As of 2014-04-16 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (434 votes), past polls