CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Verifying radio buttons

 



darkhider
Novice

Dec 11, 2009, 7:34 PM

Post #1 of 20 (5013 views)
Verifying radio buttons Can't Post

Hello guys, I am trying to verify 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?

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;
}
}
}


cortecds
Novice

Dec 11, 2009, 9:44 PM

Post #2 of 20 (5002 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post

Something simple should work:
if($answer eq ""){
print "Please select an answer";
}


darkhider
Novice

Dec 11, 2009, 10:44 PM

Post #3 of 20 (4997 views)
Re: [cortecds] Verifying radio buttons [In reply to] Can't Post

So, would it look like this?

sub validatesurvey
{
$answer=1;
while (@row3=$sth->fetchrow_array())
{
if($answer eq ""){
print "Please select an answer";
answer++;
}

Another question I have is, would the $answer come from the name? or value in html?


cortecds
Novice

Dec 11, 2009, 11:16 PM

Post #4 of 20 (4990 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post

No, because $answer is the name of the variable not the value itself. So if no radio button is selected then just having:


Code
if($answer eq ""){ 
print "Please select an answer";
}

should be sufficient.

Basically your form could look like this:
<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>

The variable name would be answer and it would contain your values.

So all you would be doing is verifying if the variable had any value at all, as long as it wasn't blank you would be good to go, right?


darkhider
Novice

Dec 12, 2009, 10:46 AM

Post #5 of 20 (4946 views)
Re: [cortecds] Verifying radio buttons [In reply to] Can't Post

Okay, this is the complete code that I have so far. Maybe this would help. I have commented out 2 of the subroutines regarding cookies because they aren't working properly; I'm not sure why:


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++;
}
}



(This post was edited by darkhider on Dec 12, 2009, 10:48 AM)


cortecds
Novice

Dec 12, 2009, 10:51 AM

Post #6 of 20 (4943 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post

At a quick review, it looks like it should work.
Have you tested it? Are you having problems?


darkhider
Novice

Dec 12, 2009, 10:57 AM

Post #7 of 20 (4942 views)
Re: [cortecds] Verifying radio buttons [In reply to] Can't Post

Yes, I have tested it. It is not working properly. I am stressed out because of this :(

I have the login working. After the user logs in, it saves the cookie of the user and then he gets the survey page. He has to answer all the questions and click SendSurvey which will insert the answers in the mysql table.


(This post was edited by darkhider on Dec 12, 2009, 11:00 AM)


cortecds
Novice

Dec 12, 2009, 11:55 AM

Post #8 of 20 (4929 views)
Re: [cortecds] Verifying radio buttons [In reply to] Can't Post

Hmmm, I looked at it a little closer.
Sorry, I see what you are doing now.

A slight problem is that scalars names must start with a letters and you are assigning the name a number only.

Here is my try at it, I haven't tested it:

#!/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)
{
&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]"</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>~;
$cnt++;
}


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

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

Also you had call the subroutine "insertsurveydata" but you were trying to access it with "&insertsurveyresults;"
I have changed that in my sample above as well.
Try that.


FishMonger
Veteran / Moderator

Dec 12, 2009, 11:58 AM

Post #9 of 20 (4928 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post


Quote
I have tested it. It is not working properly.

That's a poor problem statement. You should be very specific about what is not working properly. What is it doing that it shouldn't? What is it not doing that it should? What errors/warnings are you receiving (hint: read up on the CGI::Carp module)?

My last comment in your cross post should help.
http://www.codingforums.com/showthread.php?t=184379


FishMonger
Veteran / Moderator

Dec 12, 2009, 12:00 PM

Post #10 of 20 (4927 views)
Re: [cortecds] Verifying radio buttons [In reply to] Can't Post

cortecds,

Take note; this is a homework assignment. Please don't provide full solutions. We can give guidance, but not do someones homework.


(This post was edited by FishMonger on Dec 12, 2009, 12:00 PM)


darkhider
Novice

Dec 12, 2009, 12:18 PM

Post #11 of 20 (4922 views)
Re: [cortecds] Verifying radio buttons [In reply to] Can't Post

Thank you cortecds. I will try the fixes. Monger, this isn't quite an assignment. I am doing a lab which I need to understand. So after I make this work, I can see how I would need to do it in the future. I am still practicing perl.


FishMonger
Veteran / Moderator

Dec 12, 2009, 12:37 PM

Post #12 of 20 (4917 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post

As I stated in the cross post and to the benefit of others, a lab for a class falls into the same category as homework. At least as far as these forums are concerned. Your instructor may or may not have the same opinion.

The instructor for the course I took would find this type of post grounds to drop the student from the class.


darkhider
Novice

Dec 12, 2009, 12:51 PM

Post #13 of 20 (4915 views)
Re: [FishMonger] Verifying radio buttons [In reply to] Can't Post

I have posted your reply on CF, Monger.

Once again, after modifications and tweaking, I am not able to click SubmitSurvey. It should at least do something right? Nothing happens when I click SubmitSurvey.

Here is the updated code.


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++;
}
}



(This post was edited by darkhider on Dec 12, 2009, 12:56 PM)


FishMonger
Veteran / Moderator

Dec 12, 2009, 12:57 PM

Post #14 of 20 (4909 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post


Quote
Nothing happens when I click SubmitSurvey.


That's because your submit button is outside of the form definition.


darkhider
Novice

Dec 12, 2009, 1:00 PM

Post #15 of 20 (4907 views)
Re: [FishMonger] Verifying radio buttons [In reply to] Can't Post

I'm not quite sure what you mean. After the user logs in. I display the survey. So now, when the user clicks SendSurvey, it should work because I have it after the if statement of the Login. I have tried using elsif for 2nd if statement, but that's not working either.


(This post was edited by darkhider on Dec 12, 2009, 1:03 PM)


cortecds
Novice

Dec 12, 2009, 1:07 PM

Post #16 of 20 (4902 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post

  
Move the form close tag to after the submit button
<input type="submit" value="SendSurvey" name=submit> </form>


darkhider
Novice

Dec 12, 2009, 1:12 PM

Post #17 of 20 (4901 views)
Re: [cortecds] Verifying radio buttons [In reply to] Can't Post

Okay, i have tried it and still not working. SendSurvey button still does not do anything.

So last couple of lines of DisplaySurvey looks like this:

Code
         <input type="hidden" name="id" value="$row[0]"> 
</td>
</tr>~;
$cnt++;
}


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


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

}



(This post was edited by darkhider on Dec 12, 2009, 1:14 PM)


FishMonger
Veteran / Moderator

Dec 12, 2009, 1:23 PM

Post #18 of 20 (4897 views)
Re: [darkhider] Verifying radio buttons [In reply to] Can't Post

You need to move this line as well.

Code
<form action="survey.cgi" method="post">

It should be prior to the while loop.


darkhider
Novice

Dec 12, 2009, 1:35 PM

Post #19 of 20 (4889 views)
Re: [FishMonger] Verifying radio buttons [In reply to] Can't Post

Sorry Monger, it was my fault. I had the form action to another script. After changing it, at least clicking SendSurvey does something.

I have a question. When im sending cookie, do I need this line?
print "Content-type:text/html\n\n";

This is what I should only have, right?

Code
sub sendlogincookie 
{
# Print HTTP header including cookie
print "Set-Cookie: uid=$row[0]\n";
}



And also, I don't think the verifysurvey is still working.
When I click SendSurvey while there are no answers selected, the message should be displayed saying "Please select an answer." Instead, it just shows the survey page again.

Mysql table for answers looks like this using "desc answers":

Code
+--------+--------------+------+-----+---------+----------------+ 
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| UserID | varchar(50) | NO | | | |
| answer | varchar(500) | NO | | | |
+--------+--------------+------+-----+---------+----------------+



(This post was edited by darkhider on Dec 12, 2009, 1:46 PM)


darkhider
Novice

Dec 12, 2009, 7:17 PM

Post #20 of 20 (4861 views)
Verifying radio buttons [In reply to] Can't Post

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.


Code
 
#!/usr/bin/perl -T

use warnings;
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)
#{
&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++;
}
}



(This post was edited by darkhider on Dec 12, 2009, 8:16 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives