Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

cgi based quiz

by sargg55 (Novice)
on Apr 23, 2016 at 09:35 UTC ( #1161297=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I'm trying to create a quiz using CGI and HTML5 . Where html5 has the radio boxes and the CGI generates the correct response. I'v been at this for the past 8 hours. Im not the best at perl . Im hoping that someone could help Thanks

#!/usr/bin/perl use Modern::Perl; use warnings; use strict; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; print header(); my ($grp1,$grp2,$grp3); $grp1 = param('grp1'); $grp2 = param('grp2'); $grp3 = param('grp3'); if ($grp1 eq "Correct1" ) { print p("Question One Is Correct"); } elsif ($grp1 eq"Incorrect1") { print p(" Question One is Incorrect"); } if ($grp2 eq "Correct2") { print p("Question One Is Correct"); } elsif ($grp2 eq"Incorrect2") { print p(" Question One is Incorrect"); } if ($grp3 eq "Correct3") { print p("Q3 is correct" ) ; } elsif ($grp3 eq "Incorrect3") { print p("q3 is wrong"); } ~
HTML 5
</ul> </div> <div id="h3"> <h1> Little quiz </h1> </div> <br/> <form action="pagefive.cgi" method="get"> Q1: Intermittent Fasting determines your food diet<br/> <input type="radio" name="grp1" value="Incorrect1"> Yes<br +/> <input type="radio" name="grp1" value="Correct1"> No<br/> </form> Q2: You have two individuals, One who's diet consists +of Mcnuggets from Mcdonalds. And an invdivudal whose diet consists of + clean food. Both are at caloric deficit and are excercising. Do you +think the indivdual with the Mcnugget diet will lose weight ?<br/> <form action="pagefive.cgi" method="get"> <input type="radio" name="grp2" value="Correct2">Yes<b +r/> <input type="radio" name="grp2" value="Incorrect2">No< +br/> </form> Q3: The more over weight you are, the more you will lose w +hen you start any excercise or diet program <br/> <form action="pagefive.cgi" method="get"> <input type="radio" name="grp3" value="Correct3"> Yes< +br/> <input type="radio" name="grp3" value="Incorrect3"> No + <br/> <input type="submit" value="Submit"> </form>
as for the cgi, I tried many combinations, I just can't seem to get it right The thing is, it's not generating the correct response to the questions that were filled.

Replies are listed 'Best First'.
Re: cgi based quiz
by poj (Abbot) on Apr 23, 2016 at 10:25 UTC

    Put all the inputs in one form

    <div id="h3"> <h1> Little quiz </h1> </div> <br/> <form action="pagefive.cgi" method="get"> Q1: Intermittent Fasting determines your food diet<br/> <input type="radio" name="grp1" value="Incorrect">Yes<br/> <input type="radio" name="grp1" value="Correct">No<br/> Q2: You have two individuals, One who's diet consists of Mcnuggets f +rom Mcdonalds. And an invdivudal whose diet consists of clean food. Both are at caloric deficit and are exercising. Do you think the indivdual with the Mcnugget diet will lose weight ? +<br/> <input type="radio" name="grp2" value="Correct">Yes<br/> <input type="radio" name="grp2" value="Incorrect">No<br/> Q3: The more over weight you are, the more you will lose when you st +art any excercise or diet program <br/> <input type="radio" name="grp3" value="Correct">Yes<br/> <input type="radio" name="grp3" value="Incorrect">No<br/> <input type="submit" value="Submit"> </form>
    #!/usr/bin/perl use warnings; use strict; use CGI qw/:standard/; use CGI::Carp qw/fatalsToBrowser/; print header(); print start_html; for my $n (1..3){ print p("Question $n is ".param('grp'.$n)); } print end_html;
    poj
      Thanks. Would it be too much to ask if you could explain to me what I did wrong ? Much appreciated
      Hi, Thanks for your help. I've been at this for over 9 hours now( no lie) I just want to know, what was wrong with my old code in cgi and html5 ? the reason why I placed format thrice is because the circles would fill twice or jump. so I thought that would solve it. as for cgi, I think I did what I thought it would be logical .

        The submit sends the parameters within the form tags, ie grp3. The grp1, and grp2 parameters are contained within other forms (without a submit)

        <form action="pagefive.cgi" method="get"> <input type="radio" name="grp3" value="Correct3">Yes< +br/> <input type="radio" name="grp3" value="Incorrect3">No + <br/> <input type="submit" value="Submit"> </form>
        poj
Re: cgi based quiz
by NetWallah (Canon) on Apr 23, 2016 at 21:03 UTC
    I have not use this myself, but, if you have a large corpus of questions, you may want to check out QWizard.

            This is not an optical illusion, it just looks like one.

Re: cgi based quiz
by dbuckhal (Hermit) on Apr 25, 2016 at 01:35 UTC
    A couple of tips to help isolate issues between HTML and CGI script:
    1. Print the passed in parameters to the console, and/or
    2. Supply the values in the CGI and check the script output (shown below)
    . . . my ($grp1,$grp2,$grp3) = qw{ Correct1 Incorrect2 Correct3}; # explicit + values # $grp1 = param('grp1'); # passed in values # $grp2 = param('grp2'); # $grp3 = param('grp3'); if ($grp1 eq "Correct1" ) { print p("Question One Is $grp1"); } elsif ($grp1 eq"Incorrect1") { print p(" Question One is $grp1"); } if ($grp2 eq "Correct2") { print p("Question One Is $grp2"); } elsif ($grp2 eq"Incorrect2") { print p(" Question One is $grp2"); } if ($grp3 eq "Correct3") { print p("Q3 is $grp3" ) ; } elsif ($grp3 eq "Incorrect3") { print p("q3 is $grp3"); } __output__ Content-Type: text/html; charset=ISO-8859-1 <p>Question One Is Correct1</p><p> Question One is Incorrect2</p><p>Q3 + is Correct3</p>
    So, it would appear the script is correct, and that the problem may lie in the HTML.

    I know these are basic tips, but basic things can get easily overlooked.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-05-25 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?