Solved

create a simple yes/no question online

Posted on 2002-04-30
4
271 Views
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?

Thanks
0
Comment
Question by:Sankar030999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 3

Expert Comment

by:Tsvetomir
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)
0
 
LVL 3

Accepted Solution

by:
Tsvetomir earned 100 total points
ID: 6983013
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
 
LVL 3

Expert Comment

by:Tsvetomir
ID: 6983053
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
 

Author Comment

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

Thanks
Sankar
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

734 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