...

View Full Version : Verifying radio buttons



darkhider
12-12-2009, 09:53 AM
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.

FishMonger
12-12-2009, 04:45 PM
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.

darkhider
12-12-2009, 06:36 PM
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.



#!/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++;
}
}

FishMonger
12-12-2009, 07:11 PM
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:

#!/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.

darkhider
12-12-2009, 07:18 PM
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.

FishMonger
12-12-2009, 07:28 PM
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.

darkhider
12-12-2009, 07:32 PM
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? :(

darkhider
12-12-2009, 07:33 PM
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.

FishMonger
12-12-2009, 07:49 PM
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.

darkhider
12-12-2009, 08:20 PM
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.

FishMonger
12-12-2009, 08:30 PM
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?

darkhider
12-12-2009, 08:46 PM
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.


#!/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++;
}
}

bazz
12-12-2009, 11:07 PM
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

darkhider
12-12-2009, 11:28 PM
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.

bazz
12-12-2009, 11:31 PM
datsokay :)

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



use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);


He has explained the reason for this well, in post #4

bazz

darkhider
12-13-2009, 03:04 AM
I have been working on this for hours and still can't make it work properly.
I have commented out stuff that I am not doing right now. So whats left is insertsurveydata. It is just not inserting the data. I am not sure what I am doing wrong.



#!/usr/bin/perl -T

use warnings;
use DBI;
use Digest::MD5 qw(md5_hex md5_base64);
use CGI;

$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)
#{
&insertsurveydata;
#&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]">$row[2]</td>
<td><input type="radio" name="$answer" value="$row[3]">$row[3]</td>
<td><input type="radio" name="$answer" value="$row[4]">$row[4]</td>
<td><input type="radio" name="$answer" value="$row[5]">$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]">
</td>
</tr>~;
$cnt++;
}

print qq~</table>\n
<br><br>\n
<input type="submit" value="SendSurvey" name=submit>
<input type="hidden" name="$qty" value="$cnt">
</form>
~;

$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
{
#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++;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum