Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Verifying radio buttons

    Hello guys, i need help verifying if the user has selected at least 1 radio button or not. I'm just trying to make a survey and I want users to answer all questions through radio buttons.

    So this is my subroutine for displaysurvey:

    sub displaysurvey
    {
    #print HTTP header
    print "Content-type:text/html\n\n";


    #### Start HTML table ####
    print qq~<html>
    <head>
    <Title>Survey Questions</Title>
    </head>
    <body>
    <table border=1>
    <tr>
    <th>Survey Question</th><th>Answer 1</th><th>Answer 2</th>
    <th>Answer 3</th><th>Answer 4</th><th>Image</th>
    <tr>~;

    #SQL select statement
    $select = qq~select id,question,answer1,answer2,answer3,answer4,image from questions~;

    #Connect to mysql and create database handler $dbh
    $dbh=DBI->connect($connectionInfo,$user,$passwd);

    #Prepare mysql statement and create statement handler $sth
    $sth=$dbh->prepare($select);

    #Execute select statement
    $sth ->execute();


    $answer=1;
    while (@row=$sth->fetchrow_array())
    {
    print qq~<tr>
    <form action="survey.cgi" method="post">
    <td>$row[1]</td>
    <td><input type="radio" name="$answer" value="$row[2]"</td>
    <td><input type="radio" name="$answer" value="$row[3]"</td>
    <td><input type="radio" name="$answer" value="$row[4]"</td>
    <td><input type="radio" name="$answer" value="$row[5]"</td>
    <td><img src="http://anything.com/images/$row[6]" width="170" height="100"/></td>
    <input type="hidden" name="id" value="$row[0]">
    </form>
    </td>
    </tr>~;
    $answer++;
    }

    #### Close HTML table ####
    print qq~</table>\n
    <br><br>\n
    <input type="submit" value="Send Survey" name=submit>
    ~;


    #Disconnect from mysql database
    $dbh->disconnect();

    }


    This is looping through each record and printing them in radio buttons. So how would I make a subroutine to verify each of them?

    My displaysurvey subroutine is fine. I'm trying to make the subroutine for validatesurvey which I'm having problem with. This is what I have so far but I don't think its quite right:

    sub validatesurvey
    {
    $missing=0;
    while (@row3=$sth->fetchrow_array())
    {
    if ($row[2] = "")
    {
    print "Please select an answer";
    missing=1;
    }
    elsif ($row[3] = "")
    {
    print "Please select an answer";
    missing=1;
    }
    elsif ($row[4] = "")
    {
    print "Please select an answer";
    missing=1;
    }
    elsif ($row[5] = "")
    {
    print "Please select an answer";
    missing=1;
    }
    if ($missing == 1)
    {
    &displaysurvey;
    exit;
    }
    }
    }


    Or the other way is very simple but I don't know if this is correct either. I'm not sure if this needs to be in a loop:

    sub validatesurvey
    {
    if ($answer eq "")
    {
    print "Please select an answer";
    &displaysurvey;
    }
    }


    Any help is appreciated.
    Last edited by darkhider; 12-12-2009 at 10:56 AM.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,915
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Please put you code inside the code tags.

    You should move the opening/closing form definition tags outside of the loop. The form definition should include the form name, but it's not required. The loop should only generate the form contents i.e., the rows and cells.

    How are you handling the submit? There is no submit button in the form.

    Where and how are you importing/processing the form submission? For example, where/how are you assigning $answer?

    Where did you intend the "Please select an answer" message to be displayed?

    Please post your complete script.

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry about that; okay this is my complete script but obviously somethings are missing. I have commented out 2 of the subroutines because I just don't know how to make them work.

    Code:
    #!/usr/bin/perl -wT
    
    use DBI;
    use Digest::MD5 qw(md5_hex md5_base64);
    
    $db="";
    $user="";
    $passwd="";
    $host="";
    $connectionInfo="dbi:mysql:$db;$host";
    
    
    
    if ($ENV{REQUEST_METHOD} eq "GET")
            {
            &displaylogin;
            exit;
            }
    else
            {
            &parseform;
                             
            if ($form{submit} eq "Login")
                    {
                    &validateuser;
                    #&sendlogincookie;
                    &displaysurvey;
                    }
            if ($form{submit} eq "SendSurvey")
                    {
                    #&readcookie;
                    if (&validatesurvey)
                            {
                            &insertsurveyresults;
                            &showsurveyresults;
                            }
                    else
                            {
                            &displaysurvey;
                            }
                    }
            }
    
    
    sub parseform
            {
            read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
            @pairs = split(/&/, $buffer);
            foreach (@pairs) {
                    ($key, $value) = split(/=/);
                    $value =~ tr/+/ /;
                    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                    $ form{$key} = $value;
                    }
            }
    
    sub displaylogin
            {
            print "Content-type:text/html\n\n";
            print qq~
                    <html>
                    <head>
                    <title>Login Page</title>
                    </head>
                    <body>
                    <form action="survey.cgi" method=post>
                    <center>
                    <h2>Enter Your Username and Password</h2>
                    User Name: <input type=text name=name value="$form{name}">
                    $errors{name}
                    <br>
                    Password: <input type=password name=password>
                    $errors{password}
                    <br>
                    <input type=submit value="Login" name=submit>
                    </form>
                    </body>
                    </html>~;
            }
    
    sub validateuser
            {
    
            $select=qq~select LoginID, UserName, Password from customers where UserName = '$form{name}'~;
    
    
            $dbh=DBI->connect($connectionInfo,$user,$passwd);
            $sth=$dbh->prepare($select);
            $sth->execute();
    
            if(@row = $sth->fetchrow_array())
                    {
            
                    $cryptpasswd = md5_hex($form{password});
    
                    if ($cryptpasswd ne $row[2])
                            {
                            $errors{password}="Incorrect password";
                            
                            &displaylogin;
                            exit;
                            }
                    }
            else
                    {
                    $errors{name} = "User name not found";
                    
                    &displaylogin;
                    exit;
                    }
          } 
    
    
    sub displaysurvey
            {
            
            print "Content-type:text/html\n\n";
    
    
            
            print qq~<html>
                     <head>
                     <Title>Survey Questions</Title>
                     </head>
                     <body>
                     <table border=1>
                     <tr>
                     <th>Survey Question</th><th>Answer 1</th><th>Answer 2</th>
                     <th>Answer 3</th><th>Answer 4</th><th>Image</th>
                     <tr>~;
    
                    
                    $select = qq~select id,question,answer1,answer2,answer3,answer4,image from questions~;
    
                    $dbh=DBI->connect($connectionInfo,$user,$passwd);
                    $sth=$dbh->prepare($select);
                    $sth ->execute();
    
    
            $answer=1;
            while (@row=$sth->fetchrow_array())
                    {
                    print qq~<tr>
                            <form action="survey.cgi" method="post">
                            <td>$row[1]</td>
                            <td><input type="radio" name="$answer" value="$row[2]"</td>
                            <td><input type="radio" name="$answer" value="$row[3]"</td>
                            <td><input type="radio" name="$answer" value="$row[4]"</td>
                            <td><input type="radio" name="$answer" value="$row[5]"</td>
                            <td><img src="http://www.anything.com/images/$row[6]" width="170" height="100"/></td>
                            <input type="hidden" name="id" value="$row[0]">
                            </form>
                            </td>
                            </tr>~;
                            $answer++;
                    }
    
    
            print qq~</table>\n
                    <br><br>\n
                    <input type="submit" value="SendSurvey" name=submit>
                    ~;
    
            $dbh->disconnect();
    
            }
    
    
    sub validatesurvey
            {
            if ($answer eq "")     
                    {
                    print "Please select an answer";
                    &displaysurvey;
                    }
            }      
    
    
    sub sendlogincookie
            {
            # Print HTTP header including cookie
            print "Set-Cookie: uid=$row[0]\n";
            print "Content-type:text/html\n\n";
    
            }
    
    sub readcookie
            {
            #if ($ENV{'HTTP_COOKIE'})="";
            }
    
    sub insertsurveydata
            {
            $answer=1;
            while (@row2=$sth->fetchrow_array())
                    {
                    $insert = qq~insert into answers (UserID,answer) values(uid,$form{$answer})~;
    
                    $dbh=DBI->connect($connectionInfo,$user,$passwd);
            
                    $sth=$dbh->prepare($insert);
    
                    $sth ->execute();
    
                    $answer++;
                    }
            }
    Last edited by darkhider; 12-12-2009 at 08:01 PM.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,915
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Every Perl script should include the strict pragma and the warnings pragma instead of the -w switch. The strict pragma forces you to declare your vars before using them, which is done with the 'my' or 'our' keywords to create lexical or global vars.

    While debugging cgi scripts, you should use the CGI::Carp module and remove it when the script goes into production.

    Get rid of the parseform sub and instead use the CGI module.

    For example:
    Code:
    #!/usr/bin/perl -T
    
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    use DBI;
    use Digest::MD5 qw(md5_hex md5_base64);
    
    # I prefer this OO interface, but the CGI module also has a functional interface
    my $cgi            = CGI->new;
    my %form           = $cgi->Vars; # this replaces your parseform sub
    my $db             = "";
    my $user           = "";
    my $passwd         = "";
    my $host           = "";
    my $connectionInfo = "dbi:mysql:$db;$host";
    That's for starters. I've got a few other things I need to do first, and then will look over the rest of the script.

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thank you Monger, but I am not allowed to "use cgi;" because my teacher said so. He wants us to do it without it.

    I know it would be a lot easier to "use cgi." But my teacher said it is better for learning purposes. He wants us to do things manually.

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,915
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Pardon my bluntness but, he's an idiot.

    Rolling your own parser like that is incomplete and has been depreciated for more than 10 years.

    He wants us to do things manually.
    Then why use the DBI and Digest::MD5 modules? There are manual methods for handling those as well.

  • #7
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes, I know. It is so stupid but I have no choice. I am trying to complete this as soon as possible. Is there anyway to fix my script?

  • #8
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by FishMonger View Post

    Then why use the DBI and Digest::MD5 modules? There are manual methods for handling those as well.
    Sorry Monger. Maybe I shouldn't have said manually. But from the looks of it, he just doesn't want us to use cgi. So I guess I am alloweed to use DBI, and Digest::MD5 modules.

    What I have working right now is that the user gets the log in page. He can log in if hes registered. Now after that I am supposed to send cookie and they get the survey page. They have to answer all questions in the survey and then the answer is sent to the database of mysql when they click SendSurvey. That is what I'm really trying to do.
    Last edited by darkhider; 12-12-2009 at 08:39 PM.

  • #9
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,915
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Since this is a homework assignment, I can't/won't provide a complete solution, but I can give you some guidance.

    Use the question id for the name attribute instead of $answer.

    When validating the survey, use the name/id to retrieve the question from the db. If $form{Qid} doesn't have a value, then you know the user didn't select an answer.

  • Users who have thanked FishMonger for this post:

    darkhider (12-12-2009)

  • #10
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by FishMonger View Post
    Since this is a homework assignment, I can't/won't provide a complete solution, but I can give you some guidance.

    Use the question id for the name attribute instead of $answer.

    When validating the survey, use the name/id to retrieve the question from the db. If $form{Qid} doesn't have a value, then you know the user didn't select an answer.
    Thanks for your help Monger. However, this is not an assignment. I am trying to do a lab which has errors in it. Once I understand how to do this type of perl script, I can apply it in future.

  • #11
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,915
    Thanks
    2
    Thanked 164 Times in 159 Posts
    A lab fall into the same category, it just may not go towards your grade.

    How much or which parts of the script were given in the lab assignment and which parts did you add/change?

  • #12
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by FishMonger View Post
    A lab fall into the same category, it just may not go towards your grade.

    How much or which parts of the script were given in the lab assignment and which parts did you add/change?
    Well I was given the whole script. Things that were missing were the database, mysql statements, displaysurvey which I have done. My verifications are still not working. I have tried modifying the cookie subroutine too.

    Code:
    #!/usr/bin/perl -wT
    
    use DBI;
    use Digest::MD5 qw(md5_hex md5_base64);
    
    $db="";
    $user="";
    $passwd="";
    $host="";
    $connectionInfo="dbi:mysql:$db;$host";
    
    
    
    if ($ENV{REQUEST_METHOD} eq "GET")
            {
            &displaylogin;
            exit;
            }
    else
            {
            &parseform;
                             
            if ($form{submit} eq "Login")
                    {
                    &validateuser;
                    &sendlogincookie;
                    &displaysurvey;
                    }
            if ($form{submit} eq "SendSurvey")
                    {
                    &readcookie;
                    if (&validatesurvey)
                            {
                            &insertsurveyresults;
                            &showsurveyresults;
                            }
                    else
                            {
                            &displaysurvey;
                            }
                    }
            }
    
    
    sub parseform
            {
            read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
            @pairs = split(/&/, $buffer);
            foreach (@pairs) {
                    ($key, $value) = split(/=/);
                    $value =~ tr/+/ /;
                    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
                    $ form{$key} = $value;
                    }
            }
    
    sub displaylogin
            {
            print "Content-type:text/html\n\n";
            print qq~
                    <html>
                    <head>
                    <title>Login Page</title>
                    </head>
                    <body>
                    <form action="survey.cgi" method=post>
                    <center>
                    <h2>Enter Your Username and Password</h2>
                    User Name: <input type=text name=name value="$form{name}">
                    $errors{name}
                    <br>
                    Password: <input type=password name=password>
                    $errors{password}
                    <br>
                    <input type=submit value="Login" name=submit>
                    </form>
                    </body>
                    </html>~;
            }
    
    sub validateuser
            {
    
            $select=qq~select LoginID, UserName, Password from customers where UserName = '$form{name}'~;
    
    
            $dbh=DBI->connect($connectionInfo,$user,$passwd);
            $sth=$dbh->prepare($select);
            $sth->execute();
    
            if(@row = $sth->fetchrow_array())
                    {
            
                    $cryptpasswd = md5_hex($form{password});
    
                    if ($cryptpasswd ne $row[2])
                            {
                            $errors{password}="Incorrect password";
                            
                            &displaylogin;
                            exit;
                            }
                    }
            else
                    {
                    $errors{name} = "User name not found";
                    
                    &displaylogin;
                    exit;
                    }
          } 
    
    
    sub displaysurvey
            {
            print "Content-type:text/html\n\n";
    
            print qq~<html>
                     <head>
                     <Title>Survey Questions</Title>
                     </head>
                     <body>
                     <table border=1>
                     <tr>
                     <th>Survey Question</th><th>Answer 1</th><th>Answer 2</th>
                     <th>Answer 3</th><th>Answer 4</th><th>Image</th>
                     <tr>~;
    
                    $select = qq~select id,question,answer1,answer2,answer3,answer4,image from questions~;
    
                    $dbh=DBI->connect($connectionInfo,$user,$passwd);
    
                    $sth=$dbh->prepare($select);
    
                    $sth ->execute();
    
            $cnt= 1;
            while (@row=$sth->fetchrow_array())
                    {
                    $answer = "a".$cnt;
                    print qq~<tr>
                            <form action="survey.cgi" method="post">
                            <td>$row[1]</td>
                            <td><input type="radio" name="$answer" value="$row[2]"</td>
                            <td><input type="radio" name="$answer" value="$row[3]"</td>
                            <td><input type="radio" name="$answer" value="$row[4]"</td>
                            <td><input type="radio" name="$answer" value="$row[5]"</td>
                            <td><img src="http://anything.com/images/$row[6]" width="170" height="100"/></td>
                            <input type="hidden" name="id" value="$row[0]">
                            </form>
                            </td>
                            </tr>~;
                            $cnt++;
                    }
    
    
            print qq~</table>\n
                    <br><br>\n
                    <input type="submit" value="SendSurvey" name=submit>
                    ~;
    
    
            $dbh->disconnect();
    
            }
    
    sub validatesurvey
            {
            while ($qty > 1)
                    {
                    $answer = "a".$qty;
                    if ($answer eq "")
                            {
                            print "Please select an answer";
                            &displaysurvey;
                            }
                    $qty--;
                    }
            }
    
    sub sendlogincookie
            {
            # Print HTTP header including cookie
            print "Set-Cookie: uid=$row[0]\n";
            print "Content-type:text/html\n\n";
    
            }
    
    sub readcookie
            { 
            $old_cookie = $ENV{"HTTP_COOKIE"};
    
            # if cookie was found
            if($old_cookie)
                    {
                    ($cookie_name, $name) = split(/=/, $old_cookie);
                    # start headers
    
                    print "Content-Type: text/html\n\n";
    
                    # end of headers
                    }
            # cookies not found
            else
                    {
                    #redirect user to the form with a redirect header
                    print "Location:http://www.anything.com/survey.cgi";
                    }
            }
    
    sub insertsurveyresults
            {
            $answer=1;
            while (@row2=$sth->fetchrow_array())
                    {
                    $insert = qq~insert into answers (UserID,answer) values(uid,$form{$answer})~;
    
                    $dbh=DBI->connect($connectionInfo,$user,$passwd);
            
                    $sth=$dbh->prepare($insert);
    
                    $sth ->execute();
    
                    $answer++;
                    }
            }
    Last edited by darkhider; 12-12-2009 at 09:56 PM.

  • #13
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Interesting thread.

    Don't forget to follow FishMonger's suggestion in post #4.

    I know you said you weren't allowed to use cgi but surely, for the errors, it ain't so bad? I mean, it's just a more efficient way of checking for errors without having to trudge through the server logs?


    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #14
    New Coder
    Join Date
    Dec 2009
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by bazz View Post
    Interesting thread.

    Don't forget to follow FishMonger's suggestion in post #4.

    I know you said you weren't allowed to use cgi but surely, for the errors, it ain't so bad? I mean, it's just a more efficient way of checking for errors without having to trudge through the server logs?


    bazz
    I am not really experienced with "use cgi" since I am a beginner in perl.

  • #15
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    datsokay

    Just put this code at the top of your script as FishMonger suggested.

    Code:
    use strict;
    use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);
    He has explained the reason for this well, in post #4

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •