Solved

create a simple yes/no question online

Posted on 2002-04-30
4
229 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
  • 3
4 Comments
 
LVL 3

Expert Comment

by:Tsvetomir
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The solution worked perfectly...I just had to change in a few places.

Thanks
Sankar
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now