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: Edit Log



darkhider
Novice

Dec 12, 2009, 7:17 PM


Views: 4939
Verifying radio buttons

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)


Edit Log:
Post edited by darkhider (Novice) on Dec 12, 2009, 7:18 PM
Post edited by darkhider (Novice) 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