Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Refreashing the page in perl

Status
Not open for further replies.

skosterow

Technical User
Feb 23, 2002
135
US
Hello, I'm alittle new to perl, and im writtting a Quiz script that will allow for 5 multiple choice questions and more then one anser to each question. The problem im having (and i have looked for the answer, just probably not in the right place) is that i can not refresh an html page. here's the script thats pertanant:

if ($action eq "") {
print @header_html;
print "Content-type: text/html\n\n";
print &quot;<form method=post action=quiz.cgi>&quot;;
print &quot;Name:&quot;;
print &quot;<input type=text name=username>&quot;;
print &quot;Email:&quot;;
print &quot;<input type=text name=email>&quot;;
print &quot;<input type=hidden name=action value=Start>&quot;;
print &quot;<input type=submit value=Send>&quot;;
print &quot;</form>&quot;;
print &quot;Action1: $action&quot;;
print @footer_html;
&parse ();
print &quot;Action 2: $action&quot;;
}

if ($action eq &quot;Start&quot;) {
print &quot;\n\n&quot;;
print @header_html;
print &quot;$action&quot;;
print &quot;$user_name&quot;;
print @footer_html;
} else { }

when the script returns from the sub &parse it prints the next bit of code right below the first bit.

I know this is probably a real simple issue, if you could just direct me to someplace that has the answer im looking for i would greatly appreshiate it!

thanks in advanced!
 
Sorry I don't understand the question... what do you mean by refresh?
 
Sorry for not making my question clear.

my script first prints the form to the user..... then the parses the info from the form and sends it back to my script. $action then becomes = Start.

the next bit og html code is then printed directly BELOW the form instead of on a new page.

here is the link to the script and how it works.... you dont have to enter username or e-mail addy. just hit send and youll see what i mean...



i wouold like the HTML page to be &quot;refreashed&quot; so that only the next bit of code appears and not the first bit.
 
Ahhhh... you need multiple subroutines and a main structure for directing the logic. Generally a if/elsif/else loop.

# parse form data here

if ($action = '') { &login }
elsif ( $action = 'start') { &start }
# continue with steps

# subroutines here

Each sub would contain the html content you want displayed to the screen. I would suggest using CGI.pm to parse the form information. Once you get the hang of it, it can be extremely useful. It's just a suggestion though... I know many people who don't (They don't like the overhead...). I was helping someone lastnight and I posted some revised code. It might give you some ideas...
HTH,
Johnny
 
Thanks!

I will try it and let you know!

-scott (skosterow@yahoo.com)
 
that's what i use for my script~ =P
elsif statements are sexy
 
yauncin,

okay i changed the code....

now it dosnt even read that part of the code..... what am i doing wrong? here is the ENTIRE code....

#!/usr/bin/perl
my %In = ();

#########################################################
# Copyright (c) 2000 SKosterow.com All Rights Reserved. #
# #
# This Document was Created By: Scott Shawn Kosterow #
# This Document Created for SKosterow.com and is the #
# sole property of SKosterow.com and reproduction, #
# copying or transfer by any media type or means in #
# prohibited by laws of the United States of America #
# #
# CONTACT INFORMATION: #
# #
# Address: #
# 16080 A Radcliff Place #
# Aurora, Co 80015 #
# #
# Phone: #
# 303-353-2212 #
# #
# E-Mail: #
# webmaster@skosterow.com #
# #
#########################################################

#########################################################
# Set USER Variables
#########################################################
# Directoy Setings
$url = &quot;/skosterow.com&quot;; # ROOT Directory or URL
$dir = &quot;/bin/quiz&quot;; # Path to Quiz Directory
$this_script = &quot;/quiz.cgi&quot;; # This Scripts File Name
$quiz_file = &quot;/datfile.txt&quot;; # Quiz File Name
$header_file = &quot;/header.html&quot;; # HTML Header File Name
$footer_file = &quot;/footer.html&quot;; # HTML Footer File Name

# Other Settings
$exam_name = &quot;Test Name Here&quot;; # Exam Name
$site_name = &quot;Site Name Here&quot;; # Site Name

#########################################################
# DO NOT EDIT ANYTHING BELOW THIS LINE
#########################################################
# Counters
$c1 = 0; # Counter 1
$c2 = 0; # Counter 2
$c3 = 0; # Counter 3

# User Data
# $user_name = &quot; &quot;; # Testers name
$user_email = &quot; &quot;; # Testers E-mail Address
$user_questions = &quot; &quot;; # Number of questions User wants to take
$date = scalar localtime(time); # Get Date for exam

# Set Other Data Files Used
$practice_data = &quot;/practice-data.txt&quot;; # Practice Exam Questions

# Set Directories
$data = &quot;/data&quot;; # Data Directory for script
$quiz_dir = &quot;$url$dir$data$quiz_file&quot;; # Quiz files full path
$header_dir = &quot;$url$dir$data$header_file&quot;; # Header file full path
$footer_dir = &quot;$url$dir$data$footer_file&quot;; # Footer file full path
$script_dir = &quot;$dir$this_script&quot;; # This Scripts full path
$practice_data_dir = &quot;$url$dir$data$practice_data&quot;; # Practice Data full path

# Other Variables and what they are
# $action = &quot;none&quot;;
@header_html = &quot; &quot;; # Header HTML
@footer_html = &quot; &quot;; # Footer HTML
@practice_data_raw = &quot; &quot;; # Practice Data Read From File
@split = &quot; &quot;; # Data splitter
$quiz_data = &quot; &quot;; # Quiz Data Array

#########################################################
# Open Needed Files
#########################################################
# Get HTML Header File
open (HEADER, &quot;$header_dir&quot;) or die &quot;Cannot open $header_dir for read :$!&quot;;
@header_html = <HEADER>;
close (HEADER);
# Get HTML Footer File
open (FOOTER, &quot;$footer_dir&quot;) or die &quot;Cannot open $footer_dir for read :$!&quot;;
@footer_html = <FOOTER>;
close (FOOTER);

#########################################################
# Start Script
#########################################################
if ($action eq &quot;&quot;) {
print @header_html;
&name_form;
print &quot;Action1: $action&quot;;
print @footer_html;
&parse ();
print &quot;Action 2: $action&quot;;
}

elsif ($action eq &quot;Start&quot;) {
print @header_html;
&logged_in;
print @footer_html;
} else { }

exit;
#########################################################
# SUBRUTINES
#########################################################
sub parse
{
return 0 unless $ENV{REQUEST_METHOD} =~ /POST/i;
my $buffer;
read(STDIN,$buffer,$ENV{CONTENT_LENGTH});
my @form_data = split(/&/,$buffer);
foreach(@form_data)
{
$_ =~ tr/+/ /;
my ($n,$v) = split(/=/,$_,2);
$n =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;,hex($1))/eg;
$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(&quot;C&quot;,hex($1))/eg;
$v =~ s/(\<.*?)(embed|object|script|applet)(.*?\>)/$1$3/gis;
if($In{$n}) { $In{$n} .= &quot;\t$v&quot;; }
else { $In{$n} = $v; }
}


$c1 = 0;
$c2 = 0;
foreach (@form_data)
{
@split = split (/\=/, @form_data[$c1]);
$data[$c2+1] = @split[0]; # Image
$data[$c2+2] = @split[1]; # Question
$c1 = $c1 + 1;
$c2 = $c2 + 2;
}
$user_name = $data[2];
$action = $data[6];
}

#########################################################
sub name_form
{
print &quot;<form method=post action=quiz.cgi>&quot;;
print &quot;Name:&quot;;
print &quot;<input type=text name=username>&quot;;
print &quot;Email:&quot;;
print &quot;<input type=text name=email>&quot;;
print &quot;<input type=hidden name=action value=Start>&quot;;
print &quot;<input type=submit value=Send>&quot;;
print &quot;</form>&quot;;
}

sub logged_in
{
print &quot;$action&quot;;
print &quot;$user_name&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top