Submitting Form Data to the WAP Server with WMLScript

Preferences - Do not show ads

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:


(validateFormEg1.wmls)

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:


(validateFormEg1_success.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="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

Contents at a Glance
Print this Web Page | | Back to Top

Feedback Form (Expand)

What do you think about this web page?






WMLScript Tutorial Table of Contents WMLScript Tutorial Contents at a Glance Preferences - Change Color Scheme Preferences - Do Not Show Ads Previous Page Next Page