create a simple yes/no question online

Posted on 2002-04-30
Last Modified: 2010-03-05
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?

Question by:Sankar030999
  • 3

Expert Comment

ID: 6982930
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)

Accepted Solution

Tsvetomir earned 100 total points
ID: 6983013
here is an example code :

>HTML page to send the yous/no :

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

> Perl - CGI cript:

# 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) {
}else {

# 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)";
<BODY bgcolor="#FFFFFF" text="#000000">
The results : $yes say "Yes" $no say "No"


# End

Expert Comment

ID: 6983053
Sorry..., better change theese two:

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


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

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

if ($answer==1) {
}else {

# with:
if ($answer==1) {
}elsif ($answer==0){


Author Comment

ID: 6987255
The solution worked perfectly...I just had to change in a few places.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question