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
Scripting Languages
Last Comment
ahoffmann
8/22/2022 - Mon
ahoffmann
#! /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);
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
ahoffmann
+ 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 ...
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.
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_f
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);