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: Beginner:
cgi.pm question: difference between param and cookie

 



ningji
Novice

Nov 2, 2012, 7:41 AM

Post #1 of 10 (2552 views)
cgi.pm question: difference between param and cookie Can't Post

hi i have a dummy question,
want to show my kid about the Fibonacci series in the web,

so when i open it, it'll display "1 1" as initial value,
there's a "next" button, if i click it, web will show "1 2".
if i click again, "2 3".

was trying to use param( ) to store the previous 2 values for the "next" button action. But no luck, :(

ppl said i need to use cookie.
My Q is, is there a way to use param( ) to store the 2 values ?


ningji
Novice

Nov 2, 2012, 7:43 AM

Post #2 of 10 (2550 views)
Re: [ningji] cgi.pm question: difference between param and cookie [In reply to] Can't Post

my code is here,


#!/usr/bin/perl -w

BEGIN { push @INC, "/usr/share/perl5"; }
BEGIN { push @INC, "/usr/share/perl5/CGI"; }


#
# Modules
#
use strict;
use warnings;
use CGI;
use CGI qw(:standard);


# new CGI form
my $q = new CGI;

my $v1 = $q->param("first_var");
my $v2 = $q->param("second_var");
my $opt = $q->param("rsubmit");

############################
# setup defaults

if($v1 eq "")
{
$v1 = 1;
}
if($v2 eq "")
{
$v2 = 1;
}
if($opt eq "Next page")
{
# $v1 = $q->param("first_var");
# $v2 = $q->param("second_var");
$v1 += $v2;
}



print $q->header();
print $q->start_html(
-title => 'Personnel Database',
-bgcolor => '#a9d0f5'
);


print $q->start_form(
-name => 'main',
-method => 'POST',
);

print $q->submit(-name=>'rsubmit', -value => 'Prev page');
print $q->submit(-name=>'rsubmit', -value => 'Next page', "second_var"=>$v1);


print $q->end_form;



$q->param("first_var" => $v2);
$q->param("second_var" => $v1);

print $q->h4($v2);
print $q->h4($v1);
print $q->h4($opt);

print $q->end_html;

exit 0;


FishMonger
Veteran / Moderator

Nov 2, 2012, 10:27 AM

Post #3 of 10 (2536 views)
Re: [ningji] cgi.pm question: difference between param and cookie [In reply to] Can't Post

The param() method is used to retrieve submitted form field values.

You could use a hidden form field that holds the current fibonacci value, which is retrieved via the param() method and used when returning the next sequence number.


ningji
Novice

Nov 2, 2012, 1:23 PM

Post #4 of 10 (2529 views)
Post deleted by ningji [In reply to]

 


FishMonger
Veteran / Moderator

Nov 2, 2012, 2:08 PM

Post #5 of 10 (2524 views)
Re: [ningji] cgi.pm question: difference between param and cookie [In reply to] Can't Post


Quote
but cannot make it work yet

That's a very poor problem description.

The script you posted should generate an html "Internal Server Error". The main reason for that is because you sent output to the browser before sending the html header.

Here are a few things that you need to do. Other changes will also need to be made, but start with these.
1) You need to rearrange some of the print statements to fix the server error.

2) You need to remove the second set of use statements.

3) No need to load the CGI module twice, so remove the second one.

After that, try it again and post back with detailed info on how "it doesn't work".


ningji
Novice

Nov 2, 2012, 2:47 PM

Post #6 of 10 (2518 views)
Re: [FishMonger] cgi.pm question: difference between param and cookie [In reply to] Can't Post

Hi FishMonger,

sorry my copy paste screwed up.
anyway here it is again.
The problem is webpage only displays 1 and 2, no matter how many clicks you do. (i was expecting 1 1, then 1 2, then 2 3 ...)

#!/usr/bin/perl -w

BEGIN { push @INC, "/usr/share/perl5"; }
BEGIN { push @INC, "/usr/share/perl5/CGI"; }

use strict;
use warnings;
use CGI;
use CGI qw(:standard);

my $q = new CGI;

my $v1 = $q->param("first_var");
my $v2 = $q->param("second_var");
my $v3=0;
my $opt = $q->param("rsubmit");

############################
if($opt eq "Next page")
{
$v3 = $v1 + $v2;
$v1 = $v2;
$v2 = $v3;

$q->param(-name=>"first_var", -value => $v1);
$q->param(-name=>"second_var", -value => $v2);
}

############################
print $q->header();
print $q->start_html( );

print $q->h4($v1);
print $q->h4($v2);
print $q->h4($opt);

print $q->start_form(
-name => 'main',
-method => 'POST',
);

print $q->hidden(-name=>"first_var", -default=>1, -override=>1);
print $q->hidden(-name=>"second_var", -default=>1, -override=>1);
print $q->submit(-name=>'rsubmit', -value => 'Prev page');
print $q->submit(-name=>'rsubmit', -value => 'Next page');

print $q->end_form;
print $q->end_html;

exit 0;


(This post was edited by ningji on Nov 2, 2012, 2:49 PM)


FishMonger
Veteran / Moderator

Nov 2, 2012, 5:53 PM

Post #7 of 10 (2505 views)
Re: [ningji] cgi.pm question: difference between param and cookie [In reply to] Can't Post


Code
#!/usr/bin/perl -w                                              

BEGIN { push @INC, "/usr/share/perl5"; }
BEGIN { push @INC, "/usr/share/perl5/CGI"; }

Those 2 BEGIN blocks would be better written as a single use lib statement

Code
use lib '/usr/share/perl5', '/usr/share/perl5/CGI';



Code
use strict;            
use warnings;
use CGI;
use CGI qw(:standard);

That's good with the exception of the last line. It's not needed or wanted, so remove it.


Code
                                       
my $q = new CGI;

my $v1 = $q->param("first_var");
my $v2 = $q->param("second_var");
my $v3=0;
my $opt = $q->param("rsubmit");

############################
if($opt eq "Next page")
{
$v3 = $v1 + $v2;
$v1 = $v2;
$v2 = $v3;

Var names should give an indication of (describe) what kind of data they hold. Var names of $v1, $v2, and $v3 don't say anything about what they are used for.


Code
   $q->param(-name=>"first_var", -value => $v1);  
$q->param(-name=>"second_var", -value => $v2);
}

What do you expect these lines to do? I can assure you that they don't do what you expect and are not needed/wanted. Remove them.


Code
                                                  
############################
print $q->header();
print $q->start_html( );

print $q->h4($v1);
print $q->h4($v2);
print $q->h4($opt);

print $q->start_form(
-name => 'main',
-method => 'POST',
);

As stated above, those var names should be fixed.


Code
                                                                 
print $q->hidden(-name=>"first_var", -default=>1, -override=>1);
print $q->hidden(-name=>"second_var", -default=>1, -override=>1);
print $q->submit(-name=>'rsubmit', -value => 'Prev page');
print $q->submit(-name=>'rsubmit', -value => 'Next page');

print $q->end_form;
print $q->end_html;

exit 0;

Here's where the problem you were referring to is located. The values for those two hidden fields are hard coded, which is why the results never change. You need to use the updated vars that you assigned above.


ningji
Novice

Nov 2, 2012, 7:24 PM

Post #8 of 10 (2501 views)
Re: [FishMonger] cgi.pm question: difference between param and cookie [In reply to] Can't Post

it's working, :) Thanks very much !

but this is what i don't understand,


$q->param(-name=>"first_var", -value => $v1);
$q->param(-name=>"second_var", -value => $v2);
}
What do you expect these lines to do? I can assure you that they don't do what you expect and are not needed/wanted. Remove them.


thought these 2 lines are updating the values for "first_var", "second_var".
this is what param( ) for.

is it because these 2 lines are not inside the form block ?


(This post was edited by ningji on Nov 2, 2012, 7:50 PM)


FishMonger
Veteran / Moderator

Nov 3, 2012, 6:51 AM

Post #9 of 10 (2421 views)
Re: [ningji] cgi.pm question: difference between param and cookie [In reply to] Can't Post


In Reply To
it's working, :) Thanks very much !

but this is what i don't understand,


$q->param(-name=>"first_var", -value => $v1);
$q->param(-name=>"second_var", -value => $v2);
}
What do you expect these lines to do? I can assure you that they don't do what you expect and are not needed/wanted. Remove them.


thought these 2 lines are updating the values for "first_var", "second_var".
this is what param( ) for.

is it because these 2 lines are not inside the form block ?


Sounds like you need to read the CGI documentation more carefully.

The param() method is used (almost exclusively) to RETRIEVE the SUBMITTED form field data. It is also overloaded so that you can add/update/set a parameter that is used during this execution of the script but is never used to update a form field that the user sees and sends back for the next invocation of the script.

In your case, you're updating the values of those params, but never use them after that, which makes it pointless.

The hidden form fields are doing what you think those 2 param() statements are doing.


(This post was edited by FishMonger on Nov 3, 2012, 6:52 AM)


ningji
Novice

Nov 5, 2012, 7:14 AM

Post #10 of 10 (2405 views)
Re: [FishMonger] cgi.pm question: difference between param and cookie [In reply to] Can't Post

Thanks very much !


In Reply To

In Reply To
it's working, :) Thanks very much !

but this is what i don't understand,


$q->param(-name=>"first_var", -value => $v1);
$q->param(-name=>"second_var", -value => $v2);
}
What do you expect these lines to do? I can assure you that they don't do what you expect and are not needed/wanted. Remove them.


thought these 2 lines are updating the values for "first_var", "second_var".
this is what param( ) for.

is it because these 2 lines are not inside the form block ?


Sounds like you need to read the CGI documentation more carefully.

The param() method is used (almost exclusively) to RETRIEVE the SUBMITTED form field data. It is also overloaded so that you can add/update/set a parameter that is used during this execution of the script but is never used to update a form field that the user sees and sends back for the next invocation of the script.

In your case, you're updating the values of those params, but never use them after that, which makes it pointless.

The hidden form fields are doing what you think those 2 param() statements are doing.


 
 


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

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