Learning WML - Variables and Scripting
This series of articles describes how to provide Web content to mobile devices through WML (Wireless Markup Language). This article covers variables and beginning scripting with WMLScript. Future articles will cover more advanced scripting and how to integrate other technologies such as PHP to make your pages more flexible.
Note: These articles cover WML and WMLScript version 1.1, which are supported by the majority of mobile devices in use today. The articles assume a working knowledge of HTML and general Web technologies, and further assume that you have read the previous article(s) in this series.
WML supports variables that can hold transitional data between cards, provide custom output tailored to individual users, and more.
Variables are special holding places for values. They can hold numeric or alpha values and their values can be changed by code at the programmer's whim. However, it is usually good practice to dedicate variables to particular purposes, and hence, to particular types of data.
Variables in WML consist of words, enclosed in parentheses, prefixed with a dollar sign ($). For example:
$(name) $(address) $(link) $(target_url) $(_method)
Note: Because the dollar sign ($) is used to signify a variable, it is a reserved character in WML. If you want an actual dollar sign to appear anywhere in your card(s), use a double dollar sign instead ("$$").
Variable names must start with a letter or underscore. Subsequent characters can be alpha, numeric, or underscores. Variables are case sensitive; "phone_number" is different from "Phone_Number."
Wherever a variable is referenced, WML will substitute the value of the variable where the variable name appears. For example, the following code:
<p> Hello $(name)! </p>
would produce the following result, if "David" was stored in the variable $(name):
Note: WML reserves the ampersand symbol (&) for entities. To use this symbol in your deck, you must substitute the corresponding entity "&".
Variables can be set by the following elements:
<input> <select>/<option> <setvar>
Each of these elements is discussed in more detail below. Note that some of these elements were introduced in previous articles, and their parameters and such will not be detailed here.
The <input> Element
The <input> element is straightforward; it accepts user input and stores it in the variable specified. For example, the following code would accept input for a phone number and store it in the variable $(phone_number). The number is then displayed on the second card.
<?xml version="1.0"?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"> <wml> <card id="card1" title="Enter Number"> <do type="accept" label="Next"> <go href="#card2" /> </do> <p> What is your phone number? <input name="phone_number" type="text" format="(NNN) NNN-NNNN" /> </p> </card> <card id="card2" title="Display Number"> <p> Your number is:<br/> $(phone_number) </p> </card> </wml>
Note: The <input> element accepts a "value" attribute that can set a default value for the input. However, if the variable specified has already been assigned a value, the "value" attribute is ignored.
Page 1 of 3