Getting user data

Dynamic content: exporting data to a CGI script

In section 1, I hinted that WML sites would interact with server script languages involving CGI and Perl. In section 3, I gave an example WML document that used the <do> tag and the child tag <go> which serves as a replacement tag for the <form> tag of HTML. However, creating a form that a user can complete over a WAP connection is not as simple as replacing the tag.

The limitations of the WAP phone impose restrictions on the layout of your forms and how the code is formed. Most of the functions are available but many use different techniques or tags in the WML code.

Although the data sent from the WAP phone to the script on your server will appear the same as data from an equivalent HTML form, you will need to change the script so that all output, especially error messages, are printed using the WAP mime type NOT text/html. This will be looked at in a later section.

The first thing to understand about WAP forms is the use of a <postfield> tag to define the data that is sent. In HTML, every input control with a name attribute is automatically part of the output to the script. In WML you must explicitly state which fields are to send output. If you omit the field from the <postfield> section, no data will be sent. The <fieldset> tag defines which fields and which text are to be displayed together when there is not enough room for the whole card.

Another difference reflects the lack of general support for WMLScript at the moment - the WAP phone does not have the ability of a web browser to validate the data. WML does have an extra attribute to help with validation - emptyok=(true|false) default=false - to prevent empty fields being missed. Input controls can also make use of the format attribute to ensure entry of only numerical, upper case, lower case, or a limited number of characters. Any further validation must be done by the script on your server

For more information on the detail of the format attribute, see Professional XML p751.

Input tags are also restricted. Of the text input controls you can only use input type=text or type=password. Select, option and optgroup are available but each phone implements the selection box in a different way and some allow very few characters to be displayed when identifying each option. No other HTML form tags have equivalent WML tags. Submit and reset controls have to explicitly coded as <do> elements. When creating WML forms, it is essential to check the operation of the form using as many toolkits and preferably WAP phones as possible.

There are additional attributes for all tags, particularly the title attribute that is essential for all elements.

The following WML document contains just one card together with a form with three input controls for the user to complete. It has only been cosmetically altered from the one used on the codehelp WAP site. Note that the name and variable of the <postfield> tag match the name assigned to the relevant input control. Note also that only the last input control, the description of the site to be linked, is marked as optional. If any one of the other fields is empty, choosing "Add this link" - the customised replacement for type=Submit - from the WAP phone menu will cause an error.

<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
 "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
   <template>
      <do type="prev" label="Back">
         <prev/>
      </do>
   </template>
   <card id="main" title="CodeHelp" newcontext="false">
      <do type="accept" label="Add this link">
         <go method="post"
  href="http://www.codehelp.co.uk/cgi-bin/myscript.pl">
            <postfield name="title" value="$title"/>
            <postfield name="url" value="$url"/>
            <postfield name="desc" value="$desc"/>
         </go>
      </do>
      <p>WML Add link page</p>
      <p>
         All titles must be
         <b>20 characters</b>
          or less.
      </p>
      <p>
         <fieldset>Title:
            <input type="text" name="title" size="20"
      emptyok="false"/>
         </fieldset>
      </p>
      <p>The URL you post
         <b>MUST</b>
          specify a .wml file in a valid location.</p>
      <p>
         <fieldset>URL:
            <input type="text" name="url" size="100"
      emptyok="false"/>
         </fieldset>
      </p>
      <p>Make sure you include in your description all the
    searchable keywords people may enter when
    looking for your site.</p>
      <p>
         <fieldset>Description:
            <input type="text" name="desc" size="800"
      emptyok="true"/>
         </fieldset>
      </p>
   </card>
</wml>

This file uses the <go method="POST"> instead of GET used in previous files because the data included in the <postfield> tags needs to go to a script not a WML page. Use GET for navigation and POST for dynamic script content.



This is part of www.codehelp.co.uk Copyright © 1998-2004 Neil Williams
See the file about.html for copying conditions.