Laura Lee Laura Lee - 22 days ago 9
Perl Question

(Homework) How to change HTML background (with Perl CGI) using input and cookies from user?

Everything works except the background color of the HTML page in the very last CGI. Can somebody help me please? I am very stuck. I don't know why the background color won't change.

Here is the HTML:

<table border="2" cellspacing="5" cellpadding="5">
<tr>
<td align="center">Name</td>
<td><input type="text" name="customer" size="15"></td>
</tr>
<tr>
<td align="center">Select Membership Type</td>
<td>
<input type="radio" name="membership" value="0">Life
<input type="radio" name="membership" value="1">Annual
<input type="radio" name="membership" value="2">Free Trial
</td>
</tr>
<tr>
<td align="center">Choose Background Color</td>
<td>
<select name="color">
<option value="ye">Yellow
<option value="cy">Cyan
<option value="ma">Magenta
<option value="wh">White
<option value="pi">Pink
<option value="go">Gold
<option value="pa">PapayaWhip
</select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Sign Up and Set Options"></td>
</tr>
</table>
</form>





CGI that saves user's cookie from HTML above:

I am to use an array to hold the membership types, and a hash that
uses the option tag attribute values as keys for the full color names

my $customer = param('customer');
my $chosen_membership = param('membership');
my $color = param('color');
my @membership_type = ("Life", "Annual", "Free Trial");
my %colors = ("ye" => 'Yellow',
"cy" => 'Cyan',
"ma" => 'Magenta',
"wh" => 'White',
"pi" => 'Pink',
"go" => 'Gold',
"pa" => 'Papayawhip');

my $chosen_color = $colors{$color};

my $mycookie1 = cookie(-name=>'membership',
-value=>$membership_type[$chosen_membership],
-path=>'/',
-expires=>'+7d');

my $mycookie2 = cookie(-name=>'customer',
-value=>$customer,
-path=>'/',
-expires=>'+7d');

my $mycookie3 = cookie(-name=>"color",
-value=>$chosen_color,
-path=>'/',
-expires=>'+7d');

print header(-cookie => [$mycookie1, $mycookie2, $mycookie3]);
print start_html ( -title => 'Assignment 7');
print "Thank you. Your data has been recorded</br>";
print "<a href='test3.cgi'>See member page</a>";
print end_html;


Reading the cookie and using the selected color from the HTML as the background color for the page

my $membership_name = cookie("membership");
my $customer_name = cookie("customer");
my $color_name = cookie("color");

print header, start_html;
#what the hell, how to change background color??

<body bgcolor="$color_name">;
print "<h2>Welcome back, $membership_name Member $customer_name.</h2>";
print "<h4>Site rather poor, huh? Sorry. We are working on it.</h4>";
print end_html;

Answer
<body bgcolor="$color_name">;

You're missing a print and some quotes here. I'm surprised this runs at all.

What you probably mean is:

print "<body bgcolor='$color_name'>";