Here is the full text of our PHP-enhanced web page so far:
<?
$pollchoices = array(
"rabbit"=>"The White Rabbit",
"scarecrow"=>"The Scarecrow",
"tinman"=>"The Tin Man",
"neo"=>"Neo"
);
session_start();
IF ($existingchoice = $_SESSION["choice"]):
$alreadychose = true;
ELSE:
$alreadychose = false;
ENDIF;
$filename = "/path/to/data/choices.txt";
IF (!$alreadychose && $choice = $_POST["imagine"]):
//make sure that they sent us a valid choice
IF ($pollchoices[$choice]):
//yes, this choice exists
$_SESSION["choice"] = $choice;
$alreadychose = true;
$existingchoice = $choice;
//let’s append this to a file
IF ($choicefile = fopen($filename, "a")):
fwrite($choicefile, "$choice\n");
fclose($choicefile);
ELSE:
echo "<p><b>Unable to append to file choices.</b></p>\n";
ENDIF;
ENDIF;
ENDIF;
?>
<html>
<head>
<title>The Best Imaginary Character</title>
</head>
<body>
<h1>Imaginary Fight to the Finish</h1>
<?IF ($alreadychose):?>
<p>Thanks for voting for <?echo $pollchoices[$existingchoice]?>!</p>
<?ELSE:?>
<form method="post" action="poll.php">
<p>
Please choose your favorite imaginary imaginary character:
<select name="imagine">
<option value="">Choose:</option>
<?
FOREACH ($pollchoices as $code => $name):
echo "<option value=\"$code\">$name</option>\n";
ENDFOREACH;
?>
</select>
<input type="submit" value="Submit your answer" />
</p>
</form>
<?ENDIF?>
<?
//get all of our votes
$votes = file($filename);
$votecounts = array_count_values($votes);
arsort($votecounts);
FOREACH ($votecounts as $choice => $count):
$name = $pollchoices[trim($choice)];
echo "<p>$name received $count vote(s).</p>\n";
ENDFOREACH;
?>
</body>
</html>