editprefsform.pl, version supporting online users

#!D:\Perl\bin\perl.exe 
use CGI qw(:standard); 
use CGI::Cookie;
# 
#        editprefsform.pl 
# 
# Sample code to display an adjust-movie-preferences form 
# 
# This displays a form where the user can pick and choose 
# what movie categories he would like to subscribe to. 
#
# The reason this is a perl script (instead of a static 
# page) is that we can check by default the categories the 
# person is already subscribed to. 
#
%cookies = fetch CGI::Cookie;
if ($cookies{'moviereviewID'} ) {
    # If there is a cookie for this person, then he's 
    # already made some selections. We grab this info out 
    # of the database. 
    $myID = $cookies{'moviereviewID'} -> value;
    dbmopen(%prefs,"movieuserprefs",0644) || die "
         Can't open movieprefs! $!";
    ($propername, @prefs) = split(/~:~/, $prefs{$myID});
} else {
    # OK, no cookie available. We could either have 
    # none of the categories checked, or the default 
    # ones. Here, I'm going with the default ones.
    @prefs = (1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0); 
    $default = 1;
}
print header(-'Cache-Control'=>'private');
print <<END_of_FormTop; 
<HTML> 
    <HEAD> 
        <TITLE>Get customized</TITLE> 
        <META NAME="HandheldFriendly" content="True"> 
    </HEAD> 
<BODY>
<p>Please indicate what movies you would like to receive news for:</p> 
<form name="choosemovies" action="/cgi-bin/movies2/setprefs.pl" > 
<p>
END_of_FormTop
# We iterate through each category, providing a checkbox 
# for each. If the user already has this category selected 
# in his preferences, we add a "checked" to the form.
@genrenames = ( "Action/Adventure", 
    "Art House/Indie", 
    "Classics", 
    "Comedy", 
    "Drama", 
    "Horror and Suspense", 
    "Kids and Family", 
    "Musicals", 
    "Sci-Fi/Fantasy", 
    "Special Interest", 
    "TV/Documentary" 
    );
for ($i = 0; $i<11; $i++) { 
    print "<input type=\"checkbox\" name=\"$i\" value=\"1\""; 
    if ($prefs[$i]) { 
        print " checked"; 
    } 
    print ">\n$genrenames[$i]<br>\n"; 
}
print "<p>\nPlease enter your first name.\n"; 
print "<input type=\"text\" name=\"firstname\" size=\"15\" 
    maxlength=\"15\" value=\"$propername\">\n </p>\n";
# That's it! Now we just print the rest of the page.
# Note that our submit button is a little special here. 
# We use a button that calls a "submitNoResponse" 
# function, which means the results returned by the script 
# are not sent to the Forms Manager. (This is a good 
# thing, since all we care about in this next form is 
# setting cookies and internal databases.) 
#
# The first argument to this function is the text that the 
# dialog box will display. The second argument tells us 
# "Yes, I really do want you to hide the form from the 
# Forms Manager". The third argument tells us "No, you can 
# only submit one version of this form per sync"—i.e. 
# if I fill out this form again, it will only submit the 
# most recent one the next time I sync.
print <<END_of_Page;
    <input type="button" name="submit" value="Submit"
    onClick="document.forms[0].submitNoResponse(
        'Your updated categories will be here next time you sync', 
        true,false);back();">
</form> 
</BODY> 
</HTML>
END_of_Page
# Copyright (c) 2007, iAnywhere Solutions, Inc., 
# all rights reserved. 
# 
# IANYWHERE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT 
# THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR 
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
# PURPOSE, OR NON-INFRINGEMENT. IANYWHERE SHALL NOT BE 
# LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT 
# OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR 
# ITS DERIVATIVES.