21.6. Submitting Form Data to the WAP Server with WMLScript
After passing all the checks, the submit_form() function will be called to submit the form data to the WAP server. There are several ways to implement the submit_form() function. One way is to instruct the WML browser to go to an intermediate WML card. The form submission code is placed in the onenterforward event handler. It will be executed when the intermediate WML card is loaded. The intermediate WML card is invisible from the user. Here shows the code of the submit_form() function:
function
submit_form(form_username, form_password, form_email, form_name,
form_birthday)
{
WMLBrowser.setVar("errorMsg",
"");
WMLBrowser.setVar("username",
form_username);
WMLBrowser.setVar("password",
form_password);
WMLBrowser.setVar("email",
form_email);
WMLBrowser.setVar("name",
form_name);
WMLBrowser.setVar("birthday",
form_birthday);
WMLBrowser.go("validateFormEg1_success.wml");
}
In the above WMLScript code, the line:
WMLBrowser.setVar("errorMsg", "");
is used to clear the error message since we do not want it to be displayed if the user navigates back to the form.
The lines:
WMLBrowser.setVar("username",
form_username);
WMLBrowser.setVar("password",
form_password);
WMLBrowser.setVar("email",
form_email);
WMLBrowser.setVar("name",
form_name);
WMLBrowser.setVar("birthday",
form_birthday);
is used to update the values of the WML variables username, password, email, name and birthday. Remember that the leading and trailing whitespaces contained in them have been removed at the beginning of the earlier validate() function. We will use these WML variables in the intermediate WML card.
The markup of the intermediate WML card is stored in the validateFormEg1_success.wml file:
<?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="Success">
<onevent
type="onenterforward">
<go method="get"
href="processing.asp">
<postfield
name="username" value="$(username)"/>
<postfield
name="password" value="$(password)"/>
<postfield
name="email" value="$(email)"/>
<postfield
name="name" value="$(name)"/>
<postfield
name="birthday"
value="$(birthday)"/>
</go>
</onevent>
<onevent
type="onenterbackward">
<prev/>
</onevent>
<p>
This
is an intermediate card.
</p>
</card>
</wml>
As you can see in the above WML code, besides the onenterforward event handler, we have also specified the onenterbackward event handler, which contains the <prev> task. The onenterbackward event and the <prev> task are used to prevent the user from seeing this WML card when he/she navigates backwards through the history stack.
Another way to implement the submit_form() function is to write some script to create an appropriate query string and attach it to the end of the target URL. This is equivalent to the HTTP GET submission method. Here is the WMLScript code:
function
submit_form(form_username, form_password, form_email, form_name,
form_birthday)
{
// Clear the earlier error
message
WMLBrowser.setVar("errorMsg", "");
var
url_str = "processing.asp";
url_str += "?username="
+ URL.escapeString(form_username);
url_str += "&password="
+ URL.escapeString(form_password);
url_str += "&email="
+ URL.escapeString(form_email);
url_str += "&name="
+ URL.escapeString(form_name);
url_str += "&birthday="
+ URL.escapeString(form_birthday);
WMLBrowser.go(url_str);
}
The escapeString() function of the URL standard library is used to escape the special characters contained in the parameters form_username, form_password, form_email, form_name and form_birthday. This step must be done since some characters (like ?, & and =) have special meanings in URLs and they must be escaped if they are to appear in query parameter values.
The limitation of the second way is that the submission method can only be HTTP GET. If you wish to submit a form using the HTTP POST method, you have to employ the first way, i.e. using an intermediate WML card.
Previous Page | Page 70 of 71 | Next Page |
- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data