CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Passing more than one variable in Perl CGI module

 



perlFun
User

Mar 10, 2015, 3:42 PM

Post #1 of 10 (4091 views)
Passing more than one variable in Perl CGI module Can't Post

This will be a very basic question about using Perl's CGI module.

I have a cgi script. At the top of the script, a CGI object is created as follows:

my $query_obj = CGI->new;

When the script is called initially there are no params saved. At the beginning of the script, there is a redirect call as follows:

print $query_obj->redirect($query_obj->url."?<var I want to save>=<value for that var>");

When this code is hit, the URL is called again, only with ?<var I want to save>=<value for that var> tacked on to the URL. Now, <var I want to save> is a parameter that can be accessed via: $query_obj->param("<var I want to save>");


Further down in the script, there is a dropdown menu, as follows:

$query_obj->start_form;
$query_obj->popup_menu(-name=>'a name', -values=>\@values_in_dropdown_menu);
$query_obj->submit('submit','select');
$query_obj->end_form;


When the code $query_obj->submit is hit, the CGI script is called once again, passing down the value the user selected. The problem now is, I've lost access to my <var I want to save>! How can I pass this var as well during the submit call so I can still have access to it when the page is refreshed?


(This post was edited by perlFun on Mar 10, 2015, 3:43 PM)


FishMonger
Veteran / Moderator

Mar 10, 2015, 5:13 PM

Post #2 of 10 (4083 views)
Re: [perlFun] Passing more than one variable in Perl CGI module [In reply to] Can't Post

So, you're doing a redirect to the same url just to have a param in the cgi object? That's very inefficient and totally unnecessary.

You can set the value of a param directly in the script without having to do a redirect to self.
http://search.cpan.org/~leejo/CGI-4.13/lib/CGI.pm#Setting_the_value%28s%29_of_a_named_parameter:

If you want that param to be available in the form then the obvious thing to do is include a hidden input field in the form.
http://search.cpan.org/~leejo/CGI-4.13/lib/CGI.pm#Creating_a_hidden_field


perlFun
User

Mar 10, 2015, 5:22 PM

Post #3 of 10 (4081 views)
Re: [FishMonger] Passing more than one variable in Perl CGI module [In reply to] Can't Post

No, the redirect is in there for another reason. It is a legacy script anyhow, and I'm trying to modify only a small part of it.


Thanks for your response, this makes a lot of sense. I had never known of hidden fields. This is my first venture in to any sort of web or CGI type stuff.


perlFun
User

Mar 11, 2015, 10:34 AM

Post #4 of 10 (4069 views)
Re: [FishMonger] Passing more than one variable in Perl CGI module [In reply to] Can't Post

One other question. You talk about setting the variables directly, but I guess the problem I'm having is that I am wondering how to make a variable that will exist throughout all calls of the script. For example, when that redirect is executed, the <var I want to save> is then passed in as a param since it's in the url as ?<var I want to save>=. But then if the ->submit code is executed, <var I want to save> is no longer an available param, because it's not in the url that's being called.

Is there a way to make a param, which will be available each time the script is called? I tried hidden variable, but it didn't show up in my parameter list.


(This post was edited by perlFun on Mar 11, 2015, 10:34 AM)


perlFun
User

Mar 11, 2015, 10:37 AM

Post #5 of 10 (4066 views)
Re: [FishMonger] Passing more than one variable in Perl CGI module [In reply to] Can't Post

as an example, if I do this:

print $query_obj->hidden(-name=>'hide', -default=>['value1','value2']);
my @names = $query_obj->param;

@names, which should hold all params, is empty.

Even if I do:

my $hidden = $query_obj->param('hide');

$hidden is not defined.


(This post was edited by perlFun on Mar 11, 2015, 10:38 AM)


FishMonger
Veteran / Moderator

Mar 11, 2015, 10:47 AM

Post #6 of 10 (4060 views)
Re: [perlFun] Passing more than one variable in Perl CGI module [In reply to] Can't Post

You accomplish that by using either client side cookies or server side sessions which is the best method.
http://search.cpan.org/~markstos/CGI-Session-4.48/lib/CGI/Session.pm


perlFun
User

Mar 11, 2015, 10:48 AM

Post #7 of 10 (4059 views)
Re: [FishMonger] Passing more than one variable in Perl CGI module [In reply to] Can't Post

Thanks for this link. So maybe I'm not understanding - what is the point of a hidden parameter anyway then? Won't all of the parameters from the previous session disappear at the invocation of the new session? (i.e., a redirect, or ->submit, or something like that)


perlFun
User

Mar 11, 2015, 10:57 AM

Post #8 of 10 (4057 views)
Re: [perlFun] Passing more than one variable in Perl CGI module [In reply to] Can't Post

Also, I am just trying to make sure - is this new CGI::Session module really necessary? I think what I want to accomplish is actually very simple, but perhaps my problem statement wasn't clear. This is essentially what I want to be able to do:

I want to ask the user two questions:

(1) what directory do you want to access

based on the answer to that question, I ask them another question

(2) what file in the directory do you want to access

So the way it works: the CGI script is called the first initial time. The user answers question (1) by filling in a form, and ->submit gets executed. Once that ->submit is executed, the script is being called for the second time, and I have the answer to question (1) as a param. On this second call to the script, I am giving them a new form field (a drop down menu) so they can answer question (2). They answer question (2) and press submit, so that the script is called the third time. At this third call, I want to be able to have the answers to (1) and (2) available as parameters. I hope that makes sense. (Note, I understand how to generate the drop down menus, etc., it's just understanding how to make sure I have the parameters, that I am struggling with). Also note, I don't actually care about having redirects, etc. That was just legacy code, so I had kept it in.


(This post was edited by perlFun on Mar 11, 2015, 10:58 AM)


FishMonger
Veteran / Moderator

Mar 11, 2015, 11:40 AM

Post #9 of 10 (4052 views)
Re: [perlFun] Passing more than one variable in Perl CGI module [In reply to] Can't Post

You have several options.

1) Pass the data via hidden form fields.

2) Save the data in a client side cookie.

3) Store the data on the server as session vars. The session ID is passed to and retrieve from the client in a cookie. The session ID is then used to retrieve the data stored on the server.

There are a couple other options, but the 3rd option is the most common and generally best method to retain data between cgi calls.


perlFun
User

Mar 11, 2015, 11:45 AM

Post #10 of 10 (4050 views)
Re: [FishMonger] Passing more than one variable in Perl CGI module [In reply to] Can't Post

Thanks FishMonger, I'll go thoroughly understand all of these options now so I can learn how to implement them. Thanks again!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives