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



darkhider
Novice

Dec 12, 2009, 12:51 PM


Views: 5007
Re: [FishMonger] Verifying radio buttons

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)


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