A PHP-script that generates and evaluates the form (PHP)

It is often convenient to integrate the presentation of the stimulus (see 3.0.1) and the evaluation of the response (see 3.0.1) into a single script.

The following example shows how this can be done.

<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");
?>
</body>
</html>

The example first defines two functions that would either store the information (store ()) or generate the stimulus ( show_verse). The the variable $VERSE decides which action to take. If $VERSE is defined then apparently the script is called for the second time and $VERSE has to be stored. If, however, $VERSE is not defined then the stimulus stil has to be generated.



Oliver Kirchkamp 2013-04-15