Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Simple Quiz Maker (cont.)

by Anonymous Monk
on Dec 06, 2003 at 00:36 UTC ( [id://312699]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have trouble with the following code. With lines 59-65, I need to create a third page to display the answers the user has input. Any ideas or help would be greatly appreciated. Thanks.
#!/usr/bin/perl use strict; use CGI qw(:standard); print header, start_html("Quiz Maker"), h1("Quiz Maker"); if (param()) { my $who = param("firstname"); my $who2 = param("lastname"); my $email = param("email"); my $firstquestion = param("FirstQuestion"); my $secondquestion = param("SecondQuestion"); my $thirdquestion = param("ThirdQuestion"); my $fourthquestion = param("FourthQuestion"); my $fifthquestion = param("FifthQuestion"); my $answerfirstquestion = param("AnswerFirstQuestion"); my $answersecondquestion = param("AnswerSecondQuestion"); my $answerthirdquestion = param("AnswerThirdQuestion"); my $answerfourthquestion = param("AnswerFourthQuestion"); my $answerfifthquestion = param("AnswerFifthQuestion"); my $useranswerfirstquestion = param("UserAnswerFirstQuestion"); my $useranswersecondquestion = param("UserAnswerSecondQuestion"); my $useranswerthirdquestion = param("UserAnswerThirdQuestion"); my $useranswerfourthquestion = param("UserAnswerFourthQuestion"); my $useranswerfifthquestion = param("UserAnswerFifthQuestion"); my $choicesfirstquestion = param("ChoicesFirstQuestion"); my $choicessecondquestion = param("ChoicesSecondQuestion"); my $choicesthirdquestion = param("ChoicesThirdQuestion"); my $choicesfourthquestion = param("ChoicesFourthQuestion"); my $choicesfifthquestion = param("ChoicesFifthQuestion"); print p("Thank You. Here is your quiz:"); print p("Name: $who $who2"); print hr(), start_form(); print p("Question #1: $firstquestion"); print p("Answer:", textfield("UserAnswerFirstQuestion")); print hr(), start_form(); print p("Question #2: $secondquestion"); print p("Answer:", textfield("UserAnswerSecondQuestion")); print hr(), start_form(); print p("Question #3: $thirdquestion"); print p("Answer:", textfield("UserAnswerThirdQuestion")); print hr(), start_form(); print p("Question #4: $fourthquestion"); print p("Answer:", textfield("UserAnswerFourthQuestion")); print hr(), start_form(); print p("Question #5: $fifthquestion"); print p("Answer:", textfield("UserAnswerFifthQuestion")); print hr(), start_form(); print p(submit("Submit"), reset("Reset")); print end_form(), hr(); print hr(), start_form(); print p ("Thank you for taking the quiz."); print p ("For question #1, your answer was $useranswerfirstquestio +n"); print p ("For question #2, your answer was $useranswersecondquesti +on"); print p ("For question #3, your answer was $useranswerthirdquestio +n"); print p ("For question #4, your answer was $useranswerfourthquesti +on"); print p ("For question #5, your answer was $useranswerfifthquestio +n"); } else { print hr(), start_form(); print p("What's your first name?", textfield("firstname")); print p("What's your last name?", textfield("lastname")); print p("Enter your e- mail address", textfield("email")); print hr(), start_form(); print p("What is your first question", textfield("FirstQuestion")) +; print p("Please enter the answer", textfield("AnswerFirstQuestion" +)); print hr(), start_form(); print p("What is your second question", textfield("SecondQuestion" +)); print p("Please enter the answer", textfield("AnswerSecondQuestion +")); print hr(), start_form(); print p("What is your third question", textfield("ThirdQuestion")) +; print p("Please enter the answer", textfield("AnswerThirdQuestion" +)); print hr(), start_form(); print p("What is your fourth question", textfield("FourthQuestion" +)); print p("Please enter the answer", textfield("AnswerFourthQuestion +")); print hr(), start_form(); print p("What is your fifth question", textfield("FifthQuestion")) +; print p("Please enter the answer", textfield("AnswerFifthQuestion" +)); print hr(), start_form(); print p(submit("Submit"), reset("Reset")); print end_form(), hr(); } print end_html

Replies are listed 'Best First'.
Re: Simple Quiz Maker (cont.)
by tcf22 (Priest) on Dec 06, 2003 at 02:05 UTC
    Since the internet is a stateless environment, you need some way of maintaining state.

    The options I see for you are:
    1. Pass all answers through the whole app, using hidden fields.
    2. Drop a cookie with some sort of ID, and write the results to a text file/database. Then retrieve the data based on ID on the 3rd page.
    I prefer the second option, but both should work for a relatively small application like this.

    Also, a side note, instead of
    my $who = param("firstname"); my $who2 = param("lastname"); my $email = param("email"); my $firstquestion = param("FirstQuestion"); my $secondquestion = param("SecondQuestion"); my $thirdquestion = param("ThirdQuestion"); my $fourthquestion = param("FourthQuestion"); my $fifthquestion = param("FifthQuestion"); my $answerfirstquestion = param("AnswerFirstQuestion"); my $answersecondquestion = param("AnswerSecondQuestion"); my $answerthirdquestion = param("AnswerThirdQuestion"); my $answerfourthquestion = param("AnswerFourthQuestion"); my $answerfifthquestion = param("AnswerFifthQuestion"); my $useranswerfirstquestion = param("UserAnswerFirstQuestion"); my $useranswersecondquestion = param("UserAnswerSecondQuestion"); my $useranswerthirdquestion = param("UserAnswerThirdQuestion"); my $useranswerfourthquestion = param("UserAnswerFourthQuestion"); my $useranswerfifthquestion = param("UserAnswerFifthQuestion"); my $choicesfirstquestion = param("ChoicesFirstQuestion"); my $choicessecondquestion = param("ChoicesSecondQuestion"); my $choicesthirdquestion = param("ChoicesThirdQuestion"); my $choicesfourthquestion = param("ChoicesFourthQuestion"); my $choicesfifthquestion = param("ChoicesFifthQuestion");
    You could use
    my %cgi_data = Vars(); #Vars() is imported from CGI.pm
    I just think it looks a lot cleaner. It also would allow you to simplify your form output using a loop.

    - Tom

      #Vars() is imported from CGI.pm
      Not by default, it isn't. If you want to use the procedural interface to CGI.pm you'll have to make sure the :cgi-lib functions are imported first in order to use Vars() as a function.
      </pedantic_mode>
      MB
Re: Simple Quiz Maker (cont.)
by WhiteBird (Hermit) on Dec 06, 2003 at 05:12 UTC
    I don't know about a third page, but to get your answers to print on a page that does not also display your form, you might want to give your submit button a name and pass it back to your script as a parameter. The bit you would want to print out to html would look like:

    <input type="submit" name="submit_answers" value="Submit">

    I'm sorry, I'm not sure of the syntax for creating that with CGI.pm as I don't make my forms using that method. I'm sure if you research the docs for the CGI module it will give you that information.

    You then create two subroutines in your script. The first routine would be the part to display your initial quiz screen and the second routine would be to display the answers.

    When the submit button is pressed the submit paramenter gets passed back into the script, and the sub prints out the page with the results. The bit of code to call up the routines would look something like:

    if ( $CGI->param( "submit_answers" ) ) { displayAnswers( $CGI ); #the name of your sub } else { displayQuiz( $CGI ); }

    There's a whole lot more you're going to need to know in order to get this to work. This is just a hint to give you some idea and maybe get you started in a direction. There's lots of good information here in the Monastery--do some searches and read everything you can find. If you don't know how to call a subroutine, then check the Llama or the Camel book, or do a search for that as well. HTH

Re: Simple Quiz Maker (cont.)
by Anonymous Monk on Dec 06, 2003 at 02:10 UTC
    Is there anyway of integrating a statement such as "else" to print the user's answers on a third page? I have already declared variable names for the user's answer so I thought it would be much easier because the input would be inputed into the values in the corresponding variables. Thanks.
Re: Simple Quiz Maker (cont.)
by Anonymous Monk on Dec 06, 2003 at 02:14 UTC
    Also, how would I go about using your first option? Thanks for all the help.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://312699]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found