Learning WML - Variables and Scripting
The <select> and <option> Elements
Each <option> element within a <select> can also set variables. When the user selects an option, that option's "value" parameter is stored in the variable specified by the select's name parameter. Although it sounds convoluted, it is pretty straightforward in practice. The following example would store the selected color's hex value in the variable "color":
<select name="color"> <option value="#FF0000">Red</option> <option value="#00FF00">Green</option> <option value="#0000FF">Blue</option> </select>
For example, if the user selected "Green," the variable "color" would be set to "#00FF00".
The <setvar> Element
The <setvar> element is perhaps the most pure of the variable-setting elements, because it serves the sole purpose of simply setting a variable. In the following example, the variable "color" is set to "Green":
<setvar name="color" value="Green" />
The use of <setvar> is complicated by the fact that it can only be executed upon a <go>, <prev>, or <refresh> action. As such, you cannot simply embed a <setvar> anywhere you need to set a variable. It is common practice to set variables before moving to a card where you will need them, by embedding the proper <setvar> elements within the corresponding <go> or other action element.
If you need to set variables for the current card and doing so from a previous page is impractical (for example, for the first card displayed in the deck), you can use <onevent> and <refresh> elements to force a <setvar> to execute: For example, the following code will set the appropriate variables when the user enters this card from a forward direction:
<card id="card1"> <onevent type="onenterforward"> <refresh> <setvar . . . /> <setvar . . . /> </refresh> </onevent>
Note: Unlike the other elements above, the <setvar> element sets the value of the specified variable whether or not it has previously been assigned a value.
Variables are very flexible and can be used in a variety of ways. You can store the results of user choices or status flags, display variable text, etc. Using the <postfield> element, you can even pass a variable's value to an external program.
The <postfield> element defines a name/value pair to be sent to the HTTP server with the next <go>. The <postfield> element is encapsulated within <go> elements as follows:
<go . . . > <postfield name="name" value="value" /> . . . </go>
The <go> element can include an optional "method" parameter that can be set to "get" or "post," which determines how the variable/value pairs are sent to the server. There are many differences between the two methods, but generally speaking "get" encodes the pairs in the calling URL while "post" sends the pairs in the body of the calling message. Of the two, "post" should be used wherever possible because it is more robust.
Using this method, you can pass values to CGI scripts or other external applications. For example, the following example passes the name/value pair color/Red to colorselect.cgi:
<go href="colorselect.cgi"> <postfield name="color" value="Red" /> </go>
Note that the "value" attribute is mandatory and is not ignored if the variable has been set previously. To send a value of a variable that has been set elsewhere, use the variable tag as the "value" attribute. For example, the following code will send the current value of the color variable:
<postfield name="color" value="$(color)" />
Note: You can also include variables in the URL of <go> and other navigation elements by using the standard "?name=value" notation:
<go href="colorselect.cgi?color=$(color) />
As flexible as variables are, they only serve a static purpose on their own. Although you can store a variety of information, no decision-making ability is provided to set variables or make decisions regarding the status of variables within standard WML. However, you can extend the use of variables by using WMLScript to complement your WML code.
Page 2 of 3