Link to home
Start Free TrialLog in
Avatar of sunnybrad
sunnybrad

asked on

How to loop with certain values and create next button in a form in perl/cgi

Hi All:

I have to read from a text file certain values e.g. "a1", "a2", "a3"
                                                                       "a4", "a5", "a6"
Then I have to create a form with values a1-a3 on one page page.
Then this page should have a next button which would take me to a4-a6 on the next page and so till all the read values has been exhausted.

Please help me out with complete code fragment. Thanks.

Regards

-sunnybrad
Avatar of ahoffmann
ahoffmann
Flag of Germany image

#! /usr/bin/perl -w
use Tie::File;
use CGI;
my @a;
tie @a, 'Tie::File', "path/to/text/file" or die "TIE failed: $!";
my $q=new CGI;
my $n=$q->param('n');
   $n=~s/[^0-9]//g;
print $q->header;
print $q->start_html,$q->start_form;
if($n<=$#a){
  print $a[$n];
  $n++;
  print $q->hidden(value=>$n);
  print $q->submit(value=>"next line");
} else {
  print "no more lines";
}
print $q->end_form,$q->end_html;
exit(0);
Avatar of sunnybrad
sunnybrad

ASKER

Hi ahoffmann:

Can you please give some explainations as how it works. Just some comments would help. Also if there is an alternate approach to solving this could you please write about it as well.

Regards

-sunnybrad
+ tie reads the file and stores in line-wise in an array
+ CGI handles all the usuall CGI stuff and provides function to write propper HTTP headers (see header() call) and propper
   HTML syntax (see start_html(), start_form() etc.)
+ the number of line in the array ($#a) is used to determine if a "next" button is printed or not
+ that's it, just try and enjoy

The alternate approach is to use static HTML files and a CGI to display the file, cumbersome ...
Hi Ahoffmann:

Your way is really smart. I don't have Tie file facility in my perl. Will it be too much trouble to show me how to read from file and display. Code should be intelligent enough not to display next when last line is being reached.

Also, why did I ask. I cannot change/add to the perl distribution I have. Thats the way it is here.

Look forward to your response. Thanks in advance.

Regards

-sunnybrad
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial