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:
Passing 2 parameters/arguments to a script?

 



Dave Phipps
Deleted

Jan 6, 2000, 12:03 PM

Post #1 of 4 (2518 views)
Passing 2 parameters/arguments to a script? Can't Post

I want to call a script from a form within another script (actually the script is calling itself again, but you get the idea). Here is the start of the form:

print $query->startform(action=>"$script_name/results?filename=$Answerfile", -TARGET=>"results");

One of the things that gets passed is the results of the form. The other is information after the ? in the path
above(?filename=$Answerfile). It gets taken care of by the following code at the beginning of the script:

my $Answerfile = param('filename'); #This is how the Answerfile path is passed

Do you know how I can pass both of these things (the path and the results) to this script? They seem to conflict with each other. FYI, the reason this script calls itself is because it implements frames (it
is a online quiz). One subroutine does the frame HTML, another makes the question/form window, and the last one makes results frame. Also, as you may have guessed by now, I'm using CGI.pm.

For those of you who want to see my entire script, here it is (keep in
mind, I'm new at this):
Thanks in advance for any help you guys can provide.
Dave

#!/usr/bin/perl -w
#use strict;
use CGI qw(:standard);

my $query = new CGI;

my $Answerfile = param('filename'); #This is how the Answerfile path is
passed
my $script_name = "path for script";
my (@lines, @correct, @FromUser);
my $line;
my $i;
my %hash = ();
my ($question, $answer, $other_answers, $notes);
my (@alt_answers, @temparray, @notesarray);
my $oa;
print $query->header;
my $TITLE="Dave's Sample";

# We use the path information to distinguish between calls
# to the script to:
# (1) create the frameset
# (2) create the question form
# (3) create the query results

my $path_info = $query->path_info;
#my $path_info = param('path_info');

# If no path information is provided, then we create
# a side-by-side frame set
if (!$path_info)
{
&print_frameset;
exit 0;
}

# If we get here, then we either create the question form
# or we create the results.

&print_html_header;
&print_query if $path_info=~/questions/;
&print_response if $path_info=~/results/;
&print_end;


# Create the frameset
sub print_frameset {

print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset cols="70,30">
<frame src="$script_name/questions?filename=$Answerfile"
name="questions">
<frame src="$script_name/results?filename=$Answerfile" name="results">
</frameset>
EOF
;
exit 0;
}

#This subprogram prints out the question side of the page
sub print_query
{
print "<H1>$TITLE</H1>\n";

open(INFO, "<$Answerfile" ) or die "Unable to open $Answerfile $!\n";

@lines = <INFO>;
close (INFO) or die "Unable to close $Answerfile $!\n";
$NumOfQuestions=@lines;

print
$query->startform(-action=>"$script_name/results?filename=$Answerfile",
-TARGET=>"results");
for ($i=0;$i<$NumOfQuestions;$i++)
{
@alt_answers = ();
@temparray = ();
$_=$lines[$i];
chomp;
($question, $answer, $other_answers, $notes) = split(/\|/);
#$hash{$question} = $answer;

$correct[$i] = $_;
@alt_answers = split(/\%\%/, $other_answers);
$alt_answers[3] = $answer;

push(@temparray, splice(@alt_answers, rand(@alt_answers), 1))
while @alt_answers;
@alt_answers = @temparray;


print "<P>$question<BR>",
$query->radio_group(
-name=> $i,
-Values=>[$alt_answers[3], $alt_answers[0], $alt_answers[1],
$alt_answers[2]],
-linebreak=>'yes',
-default=>' ');
}
print "<P>",$query->submit('Action','Submit');
print $query->reset;
print $query->endform;
}

#This subprogram prints out the results in the frame on the right
sub print_response
{
print "<H1>Results</H1>\n";

open(INFO, "<$Answerfile" ) or die "Unable to open $Answerfile $!\n";

@lines = <INFO>;
close (INFO) or die "Unable to close $Answerfile $!\n";
$NumOfQuestions=@lines;

$i=0;
if(param(0) eq '')
{
print "Please submit your answers.";
}
else
{
for ($i=0;$i<$NumOfQuestions;$i++)
{
@alt_answers = ();
@notesarray = ();
@temparray = ();
my $count = 0;
$_=$lines[$i];
chomp;
($question, $answer, $other_answers, $notes) = split(/\|/);
#$hash{$question} = $answer;

$correct[$i] = $answer;
@alt_answers = split(/\%\%/, $other_answers);
@notesarray = split(/\&\&/, $notes);
$alt_answers[3] = $answer;

if (param($i) eq $correct[$i])
{
print "<p>", $i+1, ") Your answer, ", param($i), " was
correct!</p>\n";
}
else
{
print "<p>", $i+1, ") Your answer, ", param($i), " was wrong! - the
correct answer was ", $correct[$i], "<br>";
$x = 0;
foreach $alt_answers (@alt_answers)
{
if(param($i) eq $alt_answers)
{
print "<br> Notes: ";
print $notesarray[$count], "</p>";
}
$count++;
} #end foreach
} #end else
} #end for loop
} #end else
} #end print_response


sub print_html_header
{
print $query->start_html($TITLE);
}


sub print_end
{
print $query->end_html;
}


Borderline
Deleted

Jan 7, 2000, 12:46 AM

Post #2 of 4 (2518 views)
Re: Passing 2 parameters/arguments to a script? [In reply to] Can't Post

Hi Dave,

There are a few problems with the code posted here. I mean you are definitely
off to a good start using CGI.pm but the methods you are using are a little
flawed.
If you want to import the :standard set of functions you do not have to make a
reference to CGI.pm to use it.
This is not the recommended way to use the module sense you could inadvertently
redefine one or more functions provided by CGI.pm.
So you would do this
use CGI;
$query = new CGI;

From there you would reference all the functions from $query.
OK now for making a frameset to print like you want it to.
You would either do a switch or a few if elsif calls at the top of your code.
$query->param() can be used to get the query string (the stuff after the ? in
the url). Here is what your frameset would look like
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


# Create the frameset
sub print_frameset {

print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset cols="70,30">
<frame src="$script_name?action=left"
name="questions">
<frame src="$script_name?action=right" name="results">
</frameset>
EOF
}</pre><HR></BLOCKQUOTE>

Now at the top of your cgi you would test $query->param('action') to see which
subroutine to call. If this is left you would print the left frame. If this was
right you would print the right frame. If there were no parameters eg.<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

if(
not $query->param) {</pre><HR></BLOCKQUOTE> you would call the frame set subroutine.
Now for when the form is submitted. I noticed you put a query string on the form
action. This is not recommended. Use a hidden field instead. Like this:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

<input type=hidden name=action value=submit></pre><HR></BLOCKQUOTE>
So at this point you would have 4 things to test at the top of your script. It
would look something like this<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if (not $query->param) { &print_frameset }
elsif ($q->param('action') eq 'left') { &print_query }
elsif ($q->param('action') eq 'right') { &print_right_frame }
elsif ($q->param('action') eq 'submit') { &print_response }
else { die "Action not understood!" }</pre><HR></BLOCKQUOTE>
I added the print_right_frame there because you will need some default frame
page for that frame. Anyway that should do it. If you need a more detailed
explanation on this or if there is something you need me to clarify let me
know.

Scott


Dave Phipps
Deleted

Jan 7, 2000, 8:47 AM

Post #3 of 4 (2518 views)
Re: Passing 2 parameters/arguments to a script? [In reply to] Can't Post

Thank you Scott for your help. I'm afraid I do need some clarification.

First of all, I don't understand how $Answerfile will be passed into the script. I do not want $Answerfile defined in the script because I want to be able to create my text answerfile and then make the online quiz by making a URL with a query string like this:
http://scriptURL/scriptname.pl?filename=path/quiz12.txt

The URL above is how I have been planning to use the script. Then, inside the script I have this to define the $Answerfile:

my $Answerfile = param('filename');

I want to be able to open and use $Answerfile in both frames. How can I pass both filename and action into my frames?

The other part I don't understand is the part about a hidden field for the form. I don't know where it would go. Could you show me what you mean by modifying the following form block?:

print $query->startform(-action=>"$script_name?action=submit", -TARGET=>"results");
print "$rec{'q'}<BR>",
$query->radio_group(
-name=> $x,
-Values=>[$rec{a}&#0124; &#0124;$rec{A}, $rec{b}&#0124; &#0124;$rec{B}, $rec{c}&#0124; &#0124;$rec{C}, $rec{d}&#0124; &#0124;$rec{D}],
-linebreak=>'yes',
-default=>' ');
print "<P>",$query->submit('Action','Submit');
print $query->reset;
print $query->endform;

The one thing that is really holding me up on this program is when I click on the submit button on the form. Those parameters get passed to the result frame, which is good. But I'm also passing the filename query string because I need to be able to look at $Answerfile in the results frame. That query string seems to conflict with the answers I submitted from the forum. Maybe you addressed this issue with the hidden field item, but I don't understand it yet.

In the meantime, I have started to make the other changes you suggested. I really appreciate all of your help.

Dave


Borderline
Deleted

Jan 7, 2000, 6:11 PM

Post #4 of 4 (2518 views)
Re: Passing 2 parameters/arguments to a script? [In reply to] Can't Post

I am sory I misunderstood. It will still be basicaly the same process though. You will pass the original variable along with hidden fields. I would modify what you said to show you this but I have never used the built in functions of CGI.pm to make html.
Here is what it would look like if I did it.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


print <<END_OF_HTML;
<FORM action=$script_name method=post target=results>
<input type=hidden name=filename value="@{[$query->param('filename')]}">
END_OF_HTML
</pre><HR></BLOCKQUOTE>
This would replace
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$query->startform(-action=>"$script_name/results?filename=$Answerfile",
-TARGET=>"results");</pre><HR></BLOCKQUOTE>
For the URL calles in the frameset you would use something like this.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset cols="70,30">
<frame src="$script_name?action=left&filename=@{[$query->escape($query->param('filename')))]}"
name="questions">
<frame src="$script_name?action=right&filename=@{[$query->escape($query->param('filename'))]}" name="results">
</frameset>
EOF
</pre><HR></BLOCKQUOTE>

Hope this helps.
Scott

[This message has been edited by Borderline (edited 01-08-2000).]

 
 


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

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