CGI-Based Poll

By Joe Burns

Use these to jump around or read it all...
[My Poll]
[The Parts]
[My Assumptions]
[The HTML File]
[The CGI Script]
[Get It On the Server]
[Alter the Script]

     This tutorial is a topic that people have been asking for, for some time. You, the surfer, log into a page and there's a small poll waiting for you. You answer, click to submit, and up pop the total responses. It's a pretty great effect.

     As with anything on HTML Goodies, getting something to work is pretty easy. The hard part is that I have to write and understand how it works. This concept took a while because it does get a little difficult. The process requires a PERL CGI script to work. Thus, you have to be able to place CGI on your server or this won't work for you.

     Now, before many of you without CGI access ask, no. You cannot get this effect through HTML or JavaScript alone. The format here requires that something (a CGI, VBScript, or possibly an applet) write a file to keep track of all the answers. Without that "count" being kept - the concept is pretty much shot.

     That said...let's see it.


My Poll


The HTML Goodies
Life Affirming
Question:


Would you go
to the North Poll?

           Yes
           No
           Maybe

     Pretty cool, huh? I'm trying to teach myself PERL at the moment (12/22/99) and this is my first really big script. I had a little help with some of the more codey parts but the rest is pure me. You should have seen the dance I did when it worked for the first time. I could have won an award.

     And before you write to tell me...yes, I know North Pole is spelled O-L-E. You see this is a "poll", so I spelled "pole", "poll". It's more of the pure comedy you have come to expect on the HTML Goodies site.

     Thanks folks! I'm here all week! Tip your waitress!


The Parts

     You're going to require six files to make this effect run: the HTML document, the PERL Script, the poll.dat file, and three images.

     Here are the first five:

     There's no real reason to give you the poll.dat file because you can easily make it yourself. Simply open your text editor. I suggest Notepad or SimpleText. Save a blank document as poll.dat. That's it. Make sure the document is blank. There can be no text on it at all. Those of you who use FrontPage or some other HTML assistant, don't use it to do this. I promise it will add code. This document must be blank.

     Now that I've driven that point into the ground, we'll go on.


My Assumptions

I Assume You Have Access

     As was stated at the beginning of this tutorial, I am assuming you have the ability to create, post and change modifications in a CGI-BIN on your server. If you are on a free server like Geocities, or a national pay-for like AOL or Compuserve, my bet is that you do not have access.

     If you're not sure if you have access, call your provider and ask. If you do not have access but want it, I'm afraid that it's probably going to cost you a monthly ISP fee to get it. Smaller, local ISPs are the way to go if you want to play with CGIs.

I Assume You've Read One Of the Other CGI Tutorials

     A good two years ago, I posted three tutorials on creating CGI-BINs and changing modifications to get PERL scripts to run. Since those three are there for the reading, I'm going to assume you've read at least one of them.

     Each explain how to create a CGI-BIN, Telnet, change modification, and a couple other funny terms I'll use in the next run of paragraphs.

     If you're reading along and you run into something you don't get, it's probably because you need to start with a more basic, ground-up, tutorial. Try them in this order if you haven't already:

     Each is a true work of art...


The HTML File

     Hopefully you've grabbed the HTML file from above. It's a simple FORM format that can be used pretty much as is. The only line you're concerned with is the first one:

<FORM METHOD="POST" ACTION="/cgi-bin/joepoll.cgi">

     I don't know the path to your server's CGI-BIN. You need to find that out and put it in there so that the output of the form can be send to the correct place. I named the CGI "joepoll.cgi". You may want to name it something else. Your server may also not be a big fan of the ".cgi" extension. You have to use the ".pl" ending. It's up to you to find these things out.

     The form is set up to allow for three answers: yes, no, and maybe. It's easy to change those three answers, it's much harder to add answers, but later I'll show you how.

     That's about it. The real fight is in the CGI script.


The CGI Script

     OK, now the fun part. The PERL script is set up to work with the HTML document you just grabbed. The two work together so if you alter one, you have to alter the other.

     When you viewed the script above, the file was in HTML format. If you downloaded it, you got an HTML document. You should have copied and pasted right from the page to your test editor for it to be correct.

     Without getting into a dissertation about PERL, the following is the script in HTML format with bold text inserted to explain what each part does. Take a look:

Click for Script Explanation

     That was just for display. Don't copy and paste that code. It just won't work and will become an error fest you'll never get out of.

     That first line is very important. That's the path to the PERL program on your server. The format in the script is the most common. I've worked with seven different servers and that path has worked on all of them.

     To see if your system is set up the same way, telnet into your server and, at the prompt, type:

whereis perl

     The path to PERL will be returned. If it's the same as above, then you're good to go. If not, change it in the script.

     If you'd like, you can alter the headers under where it reads Start printing HTML to a page for display.

     The only other item you truly have to be concerned about in the script are the paths to the three images. If you look at the script code again, look down into the multiple "print" statements. Towards the end of the three blocks is a section where the image is being written to the page. The first, the "Yes" answer image, looks like this:

print "<IMG SRC=/pollred.gif height=7 width=";

     At the moment, I have the images set to run off of the root of the server. Note the forward slash. If you run your images out of a special directory, then you'll need to alter this path to equal where the CGI can find those images.

     It is possible to only call for the images by name and run them out of the CGI directory, but I wouldn't suggest it. You don't want to start cluttering up that bin. Keep it for CGIs only.


Get It On the Server

     Now that you've altered the HTML document, altered the joepoll.cgi, and created a blank document for poll.dat, put them on the server.

  • Upload the HTML document where you post your other HTML documents.
  • Upload the CGI to the CGI-BIN.
    • Set the CGI modification to 755 (chmod 755 joepoll.cgi)
  • Upload poll.dat to the CGI-BIN
    • Set poll.dat's modification to 766 (chmod 766 poll.dat)
  • Upload the three images to where ever you set the CGI script to find them.

     Run the HTML document. It should work. You'll have to offer at least one response to each of the three answers to get all three rows of data to show up in the results display. A zero won't produce a number in the math parts so nothing is returned.


Alter the Script

Change the Answers

     The HTML document and the CGI Script are linked, so if you alter one, you'll need to alter the other. At the moment, the script is set up to allow three answers: yes, no, and maybe. Let's say you want to change the three answers. First go to the HTML document. Change the answers and change the VALUEs given to the answers.

     You want to change this so people can choose between the top three baseball players of all time. You change the first radio button from "yes" to "Ruth". That's perfectly acceptable, just make a point of everywhere "yes" appears in the CGI - you change it to "Ruth".

     HINT! Always keep a backup of the original working script!

     Do the same for each of the other two radio buttons and links within the script. Change out the titles of the page to equal what the poll will represent and you're good to go.

Add A Second Poll

     You'll first need to follow the steps above creating a whole new HTML form code and CGI that is attached to it by equaling the value in the form code to the values in the CGI. Yes, the codes can run on the same page as long as each form code is correctly attached to the CGI you made for it.

     In addition, you need to make a point of altering the second CGI so that it no longer writes to poll.dat. Now, remember that the word "POLL" appears in the script. You need not change that, but you do have to change out everywhere it reads poll.dat to whatever you decide it will write to.

     In the baseball example above, let's say you choose to write to bball.dat. It's as good as any other name. Change out poll.dat in the new cgi to bball.dat, create a blank file named bball.dat, upload it to the CGI-BIN and set its modification to 766 like you did poll.dat.

     Now you have two separate HTML codes linked to two individual CGIs each writing to two separate .dat files. It's a bit of work that demands concentration, but you'll get it. I did.

     Adding more choices and topics is a little tougher. I intend to make an entire tutorial from it. Stay tuned.

     Happy polling.

 

Enjoy!

 

[My Poll]
[The Parts]
[My Assumptions]
[The HTML File]
[The CGI Script]
[Get It On the Server]
[Alter the Script]



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
  •  
  •