learn learn learn learn - 1 year ago 109
Perl Question

Fails to call another subroutine when button is clicked using perl cgi?




Here i had tried to call the file3 subroutine when the button is clicked from the subroutine file2.How can i do it.Here is my code:-

use strict;
use warnings;
use CGI;
my $q=new CGI;
print $q->header();
html1();
my $file="file.txt";
open my $fh,'<',$file or die $!;
my($words)=file2($fh);
file2($words);
if($q->param("mem"))
{
file2($words);
}
else
{
file3($Words)
}


here i read the input file from the file handle and performed join with underscore.

sub file2
{
my $fh=shift;
my $words;
while(my $line=<$fh>)
{
my @name=grep{$)=~m/^a} $line;
$words.=join '_',@name;
}
return $words


created another subroutine to to display the second subroutine when the buttion is clicked .

sub html1
{
my ($words)[email protected]_;
print<<EOF;
<form action="sr.cgi" method="GET">
<input type="submit" name="mem" value="mem" onClick=\&file3()/>
</form>
EOF
}


this subroutine is the one which i should call from the subroutine file2 when button is clicked.

sub file3
{
my ($words)[email protected]_;
print<<"EOF";
<html><head><body>
<th>display</th>
<tr>
<td>$words</td>
</tr>
</body>
</head>
</html>
EOF
}


}

i had tried to read the sub routine file2 first and displayed it in the page.Then by using button i tried to call the subroutine file3 for its displays.I think i dont know how to call the another subroutine inside one subroutine and print the display on the same page .

Answer Source

You have this condition:

if($q->param("mem"))
{
    file2($words);
}
else
{
    file3($Words); # this should probably be $words with a small w
}

It decides when to call the sub file2 and when to call the sub file3. All you need to do is use it.

In your sub html1 you have a submit button in your HTML. It's name is mem and it will be passed to your script like that.

sub html1
{
    my ($words) = @_; # you do not need this line
    print<<EOF;
<form action="sr.cgi" method="GET">
<input type="submit" name="mem" value="mem" onClick=\&file3()/>
</form>
    EOF
}

First, the onClick attribute is meant for JavaScript. You can't put a Perl function name there. It does not mean anything. You can get rid of it.

<input type="submit" name="mem" value="mem" />

Now if you click that button, the form will submit, calling the script like this:

sr.cgi?mem=mem

Since the mem parameter is set, it will call the function file2. So clicking the button always calls file2.

If you want to call file3, you cannot include the parameter mem.

It is not clear how you would do that. The button can only do one thing. You could for example include a checkbox that decided which function to call.

<p>
If this checkbox is ticked, we will call <i>file2</i>. If it's not ticket, we will call <i>file3</i>.
</p>
<p><label>mem <input type="checkbox" name="mem" value="mem" /></label></p>
<input type="submit" name="subit" value="submit" />

Now when the Perl script gets called again, the parameter is either included or not included, and the script will decide which function to call.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download