Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So what I'm trying to do here is to build a couple for webpages. So there is main page(say m1). so m1 has just a input field and a submit button. So what I want to do is get the variable (a string) from the input field in main page and then compare it with a set of strings and then redirect to the corresponding webpage. For example, if I enter "blue" in input field, it should take the value, compare it with set of values and redirect to corresponding webpage(maybe a page with blue background)

Here is the code I'm trying with, which I got after a long search.

 use CGI qw/:standard :html3/;

 # this defines the contents of the fill out forms
 # on each page.
 @PAGES = ('Main','Blue','Red','Yellow','Green');
 %FIELDS = ('Main' => ['Name'],
            );
 # accumulate the field names into %ALL_FIELDS;
 foreach (values %FIELDS) {
 grep($ALL_FIELDS{$_}++,@$_);
 } 


 # figure out what page we're on and where we're heading.
 $current_page = calculate_page(param('page'),param('go'));
 $page_name = $PAGES[$current_page];

 print_header();
 print_form($current_page)         if $FIELDS{$page_name};
 print_review($current_page)       if $page_name eq 'Review';
 print_confirmation($current_page) if $page_name eq 'Confirmation';
 print end_html;

 # CALCULATE THE CURRENT PAGE
 sub calculate_page {
 my ($prev,$dir) = @_;
 return 0 if $prev eq '';   # start with first page
 return $prev + 1 if $dir eq 'Submit Application';
 return $prev + 1 if $dir eq 'Next Page';
 return $prev - 1 if $dir eq 'Previous Page';
 }

 # PRINT HTTP AND HTML HEADERS
 sub print_header {
 print header,
 start_html("Your Friendly Family Loan Center"),
 h1("Your Friendly Family Loan Center"),
 h2($page_name);
 }

 # PRINT ONE OF THE QUESTIONNAIRE PAGES
 sub print_form {
 my $current_page = shift;
 print "Please enter the name",
    start_form,
    hr;
 draw_form(@{$FIELDS{$page_name}});
 print hr;
 print submit(-name=>'go',-value=>'Previous Page') 
    if $current_page > 0;
 print submit(-name=>'go',-value=>'Submit'), 
    hidden(-name=>'page',-value=>$current_page,-override=>1),
    end_form;
 }

My Question is how do I access the variable that is Submitted in first page and how to compare with the set of variables ? when I say set of variables it means a array which has values red blue green yellow

share|improve this question
1  
Where did you get this code, or what resources did you use? This stuff is at least over 10 years old. Probably 15. –  simbabque Jun 3 '13 at 18:24

2 Answers 2

First, to answer your question:

You can create an object from your CGI module like this:

my $cgi = CGI->new;

Put that at the top. The object has a couple of methods. One of them is $cgi->param(). You can use it to get your form's values. So if you have a field called action, you could look at it like this:

my $action = $cgi->param('action');

In your case, you have a field called Name that you want to compare to an array.

my @colors = qw( red blue green yellow );
for (my $i=0; $ <= $#colors; $i++) {
  if ( $colors[$i] eq $cgi->param('Name') ) {
    print "<p>You have entered $colors[$i]. ";
    print "It was the $i. element in the array!</p>";
  }
}

We making an iteration variable $i that is counted from 0 to the number of elements in @colors. We are then comparing each of these elements to the form variable.

You can find more information on how this works in the manual for CGI on CPAN.


I'll not go into the details of how not very modern Perl your code is. If you want to improve, get a modern Perl book that was written after 2010. But please start with using use strict and use warnings.

share|improve this answer
    
Can you point me to a tutorial where I can learn perl for creating webpages ? from simple to complex step by step ? i searched a lot and had no luck.thanks –  mac Jun 4 '13 at 5:46
    
@mac Take a look at perl-tutorial.org. There are lots of good tutorials linked there. Make sure to read ones that have use strict and use warnings, and that do not refert to Perl as PERL as those are very old and will likely not help you. –  simbabque Jun 4 '13 at 7:35

You are looking for the redirect header

CGI.pm: Generating a Redirection Header

To elaborate, once you've computed the next URL, you add a redirect header to tell the browser to go there. There are two ways. The first way is to call redirect($new_url). The second is to call header('Location', $new_url).

I've found that sometimes, the redirect() method doesn't work for me, and I have to resort to the header() method. But that may have been my old, old version of CGI.pm.

The other thing to remember, is that neither of those methods end your request processing. They simply add a header to the response's header list. It's up to you to exit correctly from your script.

share|improve this answer
    
How did you come up with that from the question? Did I completely misunderstand the OP? –  simbabque Jun 3 '13 at 18:35
    
@simbabque, Here's how: "For example, if I enter "blue" in input field, it should take the value, compare it with set of values and redirect to corresponding webpage(maybe a page with blue background)" –  Len Jaffe Jun 3 '13 at 19:20
    
Oh yeah, makes sense. I sort of overread that part. –  simbabque Jun 4 '13 at 7:34
    
It happens. No blood, no foul. –  Len Jaffe Jun 4 '13 at 13:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.