Google Answers Logo
View Question
 
Q: CGI-Bin quiz perl script ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: CGI-Bin quiz perl script
Category: Computers > Algorithms
Asked by: cluelessone-ga
List Price: $25.00
Posted: 21 Apr 2006 08:33 PDT
Expires: 21 May 2006 08:33 PDT
Question ID: 721339
I need a simple Perl script. It should take input from a simple form,
with a list of yes/no questions. When the user submits the form, he
should then be given a score, and then the list of questions with
indication if he got the question right or not and shows the correct
answer. Each correct answer will be a .jpg image.
Answer  
Subject: Re: CGI-Bin quiz perl script
Answered By: palitoy-ga on 21 Apr 2006 09:40 PDT
Rated:5 out of 5 stars
 
Hello cluelessone-ga,

Thank-you for your question.

For the solution I have used the following <form> on an html page,
this can be easily expanded to include more questions as you require
them:

<form action="checkit.pl" method="post"
enctype="application/x-www-form-urlencoded" name="questions">
Is this correct?<input name="answer1" type="radio"
value="yes">Yes</input><input name="answer1" type="radio"
value="no">No</input><input name="Submit" type="submit" />
</form>

This form runs a perl script called checkit.pl when submitted (this
script should be placed in the same directory or folder as the html
page holding the form).  If radio button "answer1" is "yes" then it
prints yes.jpg, if radio button "answer1" is "no" then it prints
no.jpg.  Hopefully you can see how to alter this to add more questions
but feel free to ask for clarification if you need to.

The perl script is:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

# get the posted data
read(STDIN,$data,$ENV{'CONTENT_LENGTH'});
# variable for the score
$score = 0;
# loop through the data
for (split '&', $data, -1) {
  # split the current item by = (as the data comes in the
  # form x=y when submitted)
  @item = split("=",$_);
  # process "answer1" radio button
  if ( $item[0] eq "answer1" ) {
    # if the answer is no (from the radio button) then 
    # print no.jpg
    if ( $item[1] eq "no" ) { print "no.jpg"; }
    # if the answer is yes (from the radio button) then 
    # print yes.jpg and increment score by one
    elsif ( $item[1] eq "yes" ) { 
      print "yes.jpg";
      $score++;
    };
  };
  # add any more questions after this point using the same
  # format as above
};
print "<p>You have scored: ".$score."</p>";

Clarification of Answer by palitoy-ga on 21 Apr 2006 09:51 PDT
I forgot to mention, the "yes.jpg" and "no.jpg" outputs can be changed
to whatever you want.  For instance you could output "<p>The question
was 'Is this correct?' you answered 'no'.  Here is a picture: <img
src='nopicture.jpg' /></p>" or "<p>The question was 'Is this correct?'
you answered 'yes'.  Here is a picture: <img src='yespicture.jpg'
/></p>"

Request for Answer Clarification by cluelessone-ga on 22 Apr 2006 08:41 PDT
DOes this account for the case where no radio button is selected?
People may not answer the question at all and we need to account for
that I think. Also if possible I'd like to have some sort of scoring
system. Like fewer than three correct, say "you fail", 3-7 correct
"pretty good" 7-10 correct "you are a genius" if possible. Thanks.

Clarification of Answer by palitoy-ga on 22 Apr 2006 10:08 PDT
The easiest way to get a user to force them to answer is to have the
radio button automatically set to something on the form.  If this is
not done nothing is submitted when the form is sent so it is
impossible to check for it!  Maybe you could add a third radio button
to the form with the code:

<input name="answer1" type="radio" checked value="dontknow">Don't Know</input>

The script with the added "genius" part is below:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

# get the posted data
read(STDIN,$data,$ENV{'CONTENT_LENGTH'});
# variable for the score
$score = 0;
# loop through the data
for (split '&', $data, -1) {
  # split the current item by = (as the data comes in the
  # form x=y when submitted)
  @item = split("=",$_);
  # process "answer1" radio button
  if ( $item[0] eq "answer1" ) {
    # if the answer is no (from the radio button) then 
    # print no.jpg
    if ( $item[1] eq "no" ) { print "no.jpg"; }
    # if the answer is yes (from the radio button) then 
    # print yes.jpg and increment score by one
    elsif ( $item[1] eq "yes" ) { 
      print "yes.jpg";
      $score++;
    }
    else { 
      print "noanswer.jpg"; 
    };
  };
  # add any more questions after this point using the same
  # format as above
};
print "<p>You have scored: ".$score."</p>";
if ( $score <= 3 ) {
  print "<p>You fail.</p>";
}
elsif ( $score > 3 && $score < 7 ) {
  print "<p>Pretty Good.</p>";
}
else { 
  print "<p>You are a genius.</p>"; 
};

Request for Answer Clarification by cluelessone-ga on 22 Apr 2006 13:22 PDT
I don't understand how to handle answer2 and answer3 how to modify the
code to handle those parts. Which part of the code do I repeat and
which changes do I make?

Clarification of Answer by palitoy-ga on 23 Apr 2006 03:07 PDT
Thank-you for the 5-star rating and tip.  They are both appreciated.

Here is a solution for a 3 question form:

<form action="checkit.pl" method="post"
enctype="application/x-www-form-urlencoded" name="questions">
Is this correct?<input name="answer1" type="radio"
value="yes">Yes</input><input name="answer1" type="radio"
value="no">No</input><input name="answer1" type="radio"
value="maybe">Maybe</input><br />
Question 2?<input name="answer2" type="radio"
value="yes">Yes</input><input name="answer2" type="radio"
value="no">No</input><input name="answer2" type="radio"
value="maybe">Maybe</input><br />
Question 3?<input name="answer3" type="radio"
value="yes">Yes</input><input name="answer3" type="radio"
value="no">No</input><input name="answer3" type="radio"
value="maybe">Maybe</input><br />
<input name="Submit" type="submit" />
</form>

Note how the name of the radio button changes.

The perl script would then be:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

# get the posted data
read(STDIN,$data,$ENV{'CONTENT_LENGTH'});
# variable for the score
$score = 0;
# loop through the data
for (split '&', $data, -1) {
  # split the current item by = (as the data comes in the
  # form x=y when submitted)
  @item = split("=",$_);
  # process "answer1" radio button
  if ( $item[0] eq "answer1" ) {
    # if the answer is no (from the radio button) then
    # print no.jpg
    if ( $item[1] eq "no" ) { print "no.jpg"; }
    # if the answer is yes (from the radio button) then
    # print yes.jpg and increment score by one
    elsif ( $item[1] eq "yes" ) {
      print "yes.jpg";
      $score++;
    }
    else {
      print "noanswer.jpg";
    };
  };
  # process "answer2" radio button
  if ( $item[0] eq "answer2" ) {
    # if the answer is no (from the radio button) then
    # print no.jpg
    if ( $item[1] eq "no" ) { print "no.jpg"; }
    # if the answer is yes (from the radio button) then
    # print yes.jpg and increment score by one
    elsif ( $item[1] eq "yes" ) {
      print "yes.jpg";
      $score++;
    }
    else {
      print "noanswer.jpg";
    };
  };
  # process "answer3" radio button
  if ( $item[0] eq "answer3" ) {
    # if the answer is no (from the radio button) then
    # print no.jpg
    if ( $item[1] eq "no" ) { print "no.jpg"; }
    # if the answer is yes (from the radio button) then
    # print yes.jpg and increment score by one
    elsif ( $item[1] eq "yes" ) {
      print "yes.jpg";
      $score++;
    }
    else {
      print "noanswer.jpg";
    };
  };
  # add any more questions after this point using the same
  # format as above
};
print "<p>You have scored: ".$score."</p>";
if ( $score <= 3 ) {
  print "<p>You fail.</p>";
}
elsif ( $score > 3 && $score < 7 ) {
  print "<p>Pretty Good.</p>";
}
else {
  print "<p>You are a genius.</p>";
};
cluelessone-ga rated this answer:5 out of 5 stars and gave an additional tip of: $6.00

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy