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: Re: [cortecds] Verifying radio buttons: Edit Log



darkhider
Novice

Dec 12, 2009, 10:46 AM


Views: 4906
Re: [cortecds] Verifying radio buttons

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)


Edit Log:
Post edited by darkhider (Novice) on Dec 12, 2009, 10:48 AM


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

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