A PHP-script that shows two stimuli in a row and stores the results (PHP)

Introducing functions in the above example also shows how the example can easily be extended. The following example introduces another variabe, $TODO, that takes values 1 or 2 and, thus, indicates which poem should be generated. $TODO is passed from one invocation of the script to the next through HIDDEN input fields.

<html>
<body>
<?

function store ($verse) {
   echo "Thank you for contributing<P>";
   $fd = fopen ("verse.txt","a");
   fwrite($fd,"$verse\n");       
   fclose ($fd);
}

function show_verse ($vers,$todo) {
   echo "<h4>Please complete the following poem:</h4>
       $vers...<BR>
   <FORM METHOD=POST ACTION=$SCRIPT_NAME>
   <INPUT TYPE=TEXT NAME=VERSE SIZE=30>
   <INPUT TYPE=HIDDEN NAME=TODO VALUE=\"$todo\">
   <INPUT TYPE=SUBMIT VALUE=\"Fertig\">
   </FORM>";
}

if ($VERSE) store ($VERSE);

if (!$VERSE) show_verse ("To run o'er better waters hoists its sail<BR>
  The little vessel of my genius now,<BR>
  That leaves behind itself a sea so cruel;", "1");

if ($TODO==1) show_verse ("And of that second kingdom will I sing<BR>
  Wherein the human spirit doth purge itself,<BR>
  And to ascend to heaven becometh worthy.", "2");

?>
</body>
</html>



Oliver Kirchkamp 2013-04-15