?
Solved

create a simple yes/no question online

Posted on 2002-04-30
4
Medium Priority
?
281 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans
Suggested Courses

766 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