create a simple yes/no question online

I want to have a simple yes/no question on my webpage that is on unix server. Hence i have to use perl only. I just need to store the number of yes and no's. No other information like userid etc are there. Is it possible to store information in a simple txt file? Also are there any permissions that I have to set for the folder where the page and script is hosted?

How do i do this in perl?

Thanks
Sankar030999Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TsvetomirConnect With a Mentor Commented:
here is an example code :

>HTML page to send the yous/no :

<HTML>
<HEAD>
<TITLE>Vote</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF" text="#000000">
<FORM name="voteform" method="post" action="path/to/ the/script">
  .... please answer:<BR>
  yes  
  <INPUT type="radio" name="answer" value="1" checked>
    no
  <INPUT type="radio" name="answer" value="0">
</FORM>
</BODY>
</HTML>



> Perl - CGI cript:

#!/usr/bin/perl
# vote.cgi
use strict;
use warnings;
use Fcntl qw(:flock);
use Fcntl;
use CGI qw(:standard);

# Intialize variables:
# the dile with the data:
my $datafile="votes.txt";
# $yes will sotre the number of yes answers:
my $yes=0;
# $yes will sotre the number of "no" answers:
my $no=0;

# get the data from the form (the value of the radio buton: 1-yes 0-no):
$answer = param("answer");



# Open the file otr crate it if it doeasn't exists:
sysopen (FH, $datafile, O_RDWR | O_CREAT) or die "can't open/create the file $datafile :( !\n $!\n";
# Lock it to prevent errors from simultaneous tryes to write in file:
flock FH, LOCK_EX;
# Input line separator will be ";":
$/=";";
# First element is num of "yes" second of "no" answers (ie: "243;55" means 243-Yes, 55-No )    
($yes, $no)=(<FH>, 0, 0); ## 0, 0 are in case it's first time and the values are undefined

# Increment:
if ($answer==1) {
     ++$yes;
}else {
     ++$no;
}

# Update the results:
seek FH, 0 , 0;
print FH "$yes;$no";
truncate FH, tell(FH);

# release the lock and colse the file
flock FH, LOCK_UN;
close FH;

# print some results/redirect to page
print "Content-type:text/html\n\n";
print <<"(HTML)";
<HTML>
<HEAD>
<TITLE>Results</TITLE>
</HEAD>
<BODY bgcolor="#FFFFFF" text="#000000">
The results : $yes say "Yes" $no say "No"
</FORM>
</BODY>
</HTML>

(HTML)

# End
0
 
TsvetomirCommented:
Yes and it's very easy ! :)
teh clasic way to  do this is :

1. Make a html file whith the questions.
2. user submits the form with the choosen answer
3. Perl-CGI script recievies the data, opens the file and updates values;
4. print a result page back to the user or redirect him to an existent page

Yah have to set the rights :  execute (chmode 755) for the script and read/write for the data file (chmode 666)
0
 
TsvetomirCommented:
Sorry..., better change theese two:

1.)After line 29 - ($yes, $no)=(<FH>, 0, 0);
add:

chomp($yes,$no);

(removes trailing ";" from the values if it's assigned)


2.)To prevent incrmenting "no" if no answer was selected change :

if ($answer==1) {
    ++$yes;
}else {
    ++$no;
}

# with:
if ($answer==1) {
    ++$yes;
}elsif ($answer==0){
    ++$no;
}


:))
0
 
Sankar030999Author Commented:
The solution worked perfectly...I just had to change in a few places.

Thanks
Sankar
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.