;;; sec6.02.mhtml: -*- Meta-HTML -*- 

;;;  Copyright (c) 1996 Universal Access Inc.
;;;  Author: Henry Minsky (hqm@ua.com) Sun Sep 15 11:06:11 1996.
<set-var title="Handling Posted Data And Forms">
<include header.mhtml>

<plain-text>
  <pagelist-control-panel>
  <h2>Advanced Handling Of HTML Forms</h2>

  The example below shows the implementation of a forms-based guessing
  game. The user picks a secret number, and the web server tries to
  guess what it is.  The web-server makes a guess, and the user
  indicates whether the guess is too high or too low. The computer keeps
  adjusting its guess, using the principle of binary-search, until it
  narrows its guess down to the user's secret number.

  Below, the computer asks the user to think of a number between 1 and
  10 and then starts the game with a guess of 5.
</plain-text>

  <example-table result>

  <session-import default default>

  <when <not <get-var posted::action>>>
    <set-var low-limit=1 high-limit=10 last-guess=5>
    Pick a number between 1 and 9, and I will guess it.
  </when>

  <ifeq <get-var posted::action> "That's my number"
	<prog 
	  Thanks for playing!
	  <p>
	  <a href="<get-var mhtml::current-doc>">
	  Click Here To Play Another Game
	  </a>>
    <prog
      <var-case 
	posted::action = "Greater"
	<set-var low-limit=<get-var last-guess>>

	posted::action = "Less"
	<set-var high-limit=<get-var last-guess>>>

      <set-var last-guess =
	<add <get-var low-limit>
	     <div <sub <get-var high-limit>
		       <get-var low-limit>>
		  2>>>

      <form method=post action="<get-var mhtml::current-doc>">
      <font size=+2>Is your number <b><get-var last-guess></b>?</font>
      <p>
      <input type=submit name="action" value="Less">
      <input type=submit name="action" value="That's my number">
      <input type=submit name="action" value="Greater">
      </form>>>
  <session-export default default>
  </example-table>

<plain-text>
  <funref session-import> is used above to make the entire DEFAULT
  package persistent over the user's session.  We are really
  only making use of this persistence for the variables <i>low-limit,
  high-limit, and last-guess</i>.

  The computer needs to remember what the last value it guessed was,
  and what its range of guesses are, so that it can adjust its
  range based on what the user tells it in each round.

  <pagelist-control-panel>
</plain-text>
<include footer.mhtml>
