Listing 2: editprefsform.pl

For a full listing of this source code, see editprefsform.pl, basic version.

Suppose our user wants to set or alter his preferences. All we really need to do is offer a form consisting of several checkboxes (one for each genre) plus a text field for his name. This could be done with a standard HTML page. But we want to make sure the initial state of the form represents the user's current choices. (In other words, if our user has already asked to receive comedy and drama reviews, the comedy and drama fields should already be checked.) So once again, we are creating a script to produce this form.

We start with the standard headers.

#!D:\Perl\bin\perl.exe 
use CGI qw(:standard); 
use CGI::Cookie;

%cookies = fetch CGI::Cookie;

If a cookie for this person already exists, we use that information to grab his name and preferences from our database as before.

If ($cookies{'moviereviewID'} ) {
    $myID = $cookies{'moviereviewID'} -> value; 
    dbmopen(%prefs,"movieuserprefs",0644) 
        || die "Can't open movieprefs! $!"; 
    ($propername, @prefs) = split(/~:~/, 
    $prefs{$myID}); 
} else {

If no cookie is available, we could give him an entirely blank form, or present him with a form where the default categories are already checked. This is a matter of taste, really. I chose to go with the default values already checked.

@prefs = (1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0); $default = 1; }

Print out some HTML:

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 reviews for:</p> 
    <form name="choosemovies" 
    action="/cgi-bin/movies/setprefs.pl"> <p>

END_of_FormTop

Now, we print out a checkbox for each genre category. If we find that, according to our user's preferences, he is already subscribed to that category, we mark it checked by default.

@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"; }

Do something similar with the name field:

print "<p>\nPlease enter your first name.\n";
print "<input type=\"text\" name=\"firstname\" size=\"15\" 
    maxlength=\"15\" value=\"$propername\">\n </p>\n";

And we are done, except for a little clean up work. Note the Submit button right now looks like every other submit button you have seen before. We are actually going to change this in the next section, so keep that in mind.

Print <<END_of_Page;

<input type="submit" name="submit" value="Submit">

</form> </BODY> </HTML>

END_of_Page

This will give us a checklist that looks a little something like this:

Form produced by editprefsform.pl script