Task: Recording Survey Results

I l @ ve RuBoard

Now let's make a Flash movie that is somewhat useful. We'll build a slightly more complex form that asks several questions. Then, we'll send all the data to the server, where a CGI program records it. It does this by appending the data to the end of a text file.

While building this movie, refer to Figure 18.1 to get an idea of what the final movie should look like.

Figure 18.1. A simple survey form done with Flash elements.

graphics/18fig01.gif

  1. Start a new Flash movie.

  2. In this movie, place two input text fields, linked to the variables userName and userEmail .

  3. Open the movie 15radiobuttons.fla that we used in Hour 15, " User Input Elements." Copy a single radio button from that movie's stage onto the new movie's stage. This should bring with it the movie clip and two buttons as Library elements.

  4. Copy and paste to make a group of four radio buttons. Name each movie clip ageGroup1, ageGroup2, ageGroup3, and ageGroup4. Place text next to each one signifying a different age group. Select them all and choose Insert, Convert to Symbol. Make them all one movie clip named ageGroup.

  5. Repeat step 5 to make another group of four radio buttons. Name them OSmac, OSwindows, OSlinux, and OSother. Place appropriate text next to them and group them together in a movie clip named OS. At this point, you may want to check the movie 18survey.fla in the folder 18survey to see how your movie matches my sample.

  6. Add a Submit button to the bottom of the stage. Attach this script to it:

     on (release) {  sendVars = new LoadVars();  sendVars.name = userName;  sendVars.email = userEmail;  sendVars.age = ageGroup.ageGroup1.getValue();  sendVars.OS = OS.OSmac.getValue();  trace(sendVars.toString()); } 

    The getValue function is the one we made in Hour 15 that gets the name of the selected radio button. We can use any one of the button movie clips in the group to get the answer, but I used the first in each group.

  7. Run the movie. Instead of submitting the data to the server, the movie uses the trace command, along with a toString() function to output the potential submission to the Output window. This allows you to test your movie before going forward and doing the CGI script. You should see a result like this:

     OS=OSmac&age=ageGroup2&email=gary@xxx.com&name=Gary 
  8. The next step is to change the trace command to a send command. We'll use one that replaces the entire page.

     sendVars.send("survey.cgi","_self"); 
  9. The CGI program looks similar to the echo.cgi example from earlier this hour, but it writes the data to a text file instead. It then returns the text "Thank You!" as the Web page.

     #!/usr/local/bin/perl print "Content-type: text/html\n\n"; read (STDIN,$QUERY,$ENV{CONTENT_LENGTH}); open(FILE,">>survey.txt"); print FILE "$QUERY\n"; close(FILE); print "Thank you!"; 
    graphics/clock.gif

    Even if you have been able to get the echo.cgi program working on your server, you may not be able to get this one to work. This is because the program needs to have permission to create and append to a new file. You may need to adjust the permissions of the program or even the folder it sits in. You may have to create the survey.txt file yourself, as an empty text file, and then set its permissions. Some hosting providers may not allow CGI programs to create or write to files.


  10. Test this movie and then examine the new contents of survey.txt on your server. You will see the data for your test there. A new line is added every time someone takes the survey. You can compute the results of the survey using a spreadsheet program, or even another CGI script specially written to compute the results with the existing data.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net