Displaying Input Fields and Getting Data from
Users: prompt() Function
The
prompt() function can help
you get data from users. When the WMLScript interpreter encounters
the prompt() function,
it instructs the WAP browser to display an input field. The text
entered by the user will be returned from the function. This is the
syntax of prompt():
Dialogs.prompt(title,
default_text);
title
and default_text are the title and the default value of the
input field respectively. If everything works fine, prompt()
returns the text entered by the user, otherwise it returns invalid
indicating an error has occurred.
The
WML/WMLScript example below illustrates the usage of the prompt()
function. The user will be asked to enter a number in the range
1-100. If it is outside the range, we will ask the user to enter
another number.
(promptEg1.wml)
<?xml
version="1.0"?> <!DOCTYPE wml PUBLIC
"-//WAPFORUM//DTD WML 1.3//EN"
"http://www.wapforum.org/DTD/wml13.dtd">
<wml> <card
id="card1" title="prompt()
Example"> <p> Enter a number between
1-100:<br/> <input
name="number"/><br/><br/> <a
href="promptEg1.wmls#checkRange($(number))">Continue</a> </p> </card>
<card
id="card2" title="prompt() Example"> <p> The
number you entered is $(number). </p> </card> </wml>
(promptEg1.wmls)
extern
function checkRange(number) { while (number < 1 || number
> 100){ number = Dialogs.prompt("The number is not
between 1-100. Please enter again.", number); number
= Lang.parseInt(number); }
WMLBrowser.setVar("number",
number); WMLBrowser.go("#card2"); }
The
parseInt()
function in the above
script is a new function to you. It is used to convert a
string value to an integer. We will look at it in detail when we
cover the Lang standard library.
The
following screenshots show the result of the above WML/WMLScript
example in some mobile phone browsers:
|
 Sony
Ericsson T68i
|
 Nokia
Mobile Browser 4.0
|
If
you type 1000 in the input field and select the "Continue"
anchor link, the mobile phone browser will prompt you for
another number:
|
 Sony
Ericsson T68i
|
 Nokia
Mobile Browser 4.0
|
Enter
50 in the input field. The mobile phone browser will go to the WML
card with the ID "card2":
|
 Sony
Ericsson T68i
|
 Nokia
Mobile Browser 4.0
|
|
Feedback Form (ExpandCollapse)
|
|