Wireless Markup Language (WML) Tutorial

Creating Dynamic Content

You can create dynamic WML documents with any server side language such as ASP, PHP, PERL, etc. The only thing you need to do is ensure the content type has been set for WML.

In ASP, you set the content type through the Response object.

Setting the content type with ASP

Response.ContentType = "text/vnd.wap.wml"

In PHP, you set the content type through the header function.

Setting the content type with PHP

<?php
header("Content-type: text/vnd.wap.wml");
?>

A Search Page

The following describes how to create a search page for a WAP device. The first thing we need to consider is how we'll be sending data from the WML document to the server. By default, the input element uses the GET HTTP submission method. This isn't an issue with PHP, but with ASP it will mean the data will need to be retrieved through the Request object's QueryString collection. This isn't a problem, but to illustrate the difference, the following example uses the POST method to send the data. The data will need to be retrieved through the Request object's Form collection with ASP when data is submitted using this method.

search.wml

<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
    "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="index" title="Search Term">
<do type="accept" label="Search Term">
    <go href="terms.asp" method="post">
        <postfield name="term" value="$term"/>
    </go>
</do>
<p>
    Enter a search term:
    <input type="text" name="term" value=""/>
</p>
</card>
</wml>

The following is an ASP document to perform the search, and return a WML document.

terms.asp

<%@ language="vbscript" %>
<% Option Explicit %>
<% Response.ContentType = "text/vnd.wap.wml" %>
<!-- #include file="adovbs.inc" -->
<%
    Dim objDB, objRS, strConnect, strDBVirtualPath, strDBLocation, strSQL
    Dim strTerm, strDescription, counter
    strDBVirtualPath="db/terms.mdb"
    strDBLocation=Server.Mappath(strDBVirtualPath)
    strConnect="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBLocation
    Set objDB = Server.CreateObject("ADODB.Connection")
    objDB.Open strConnect
    Set objRS = Server.CreateObject("ADODB.RecordSet")
    ' Get the search term. This would be Request.QueryString("term") if
    ' no method had been specified for the input element
    strTerm = Request.Form("term")
    strSQL = "SELECT * FROM Terms WHERE [Term] LIKE '%" & strTerm & "%'"
    objRS.Open strSQL, objDB, adOpenStatic, adLockReadOnly, adCmdText
%>
<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
    "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id="term" title="Terms">
<do type="prev" label="Back">
    <prev/>
</do>
<p>
<%
    If objRS.EOF Then
        ' No terms found in the recordset
        Response.Write "Unable to retrieve any records like " & strTerm
    Else
        Response.Write "Terms with <em>" & strTerm & "</em><br/>"
        ' Get each term that matches
        Do While NOT objRS.EOF
            counter = counter + 1
            Response.Write "<em>" & objRS.Fields("Term").Value & "</em><br/>"
            strDescription = Server.HTMLEncode(objRS.Fields("Description").Value)
            Response.Write  strDescription & "<br/>"
            Response.Write "---<br/>"
            objRS.MoveNext
        Loop
        Response.Write "Retrieved " & counter & " term(s) containing " & strTerm
    End If
    ' Tidy up
    objRS.Close
    Set objRS = Nothing
    objDB.Close
    Set objDB = Nothing
%>
</p>
</card>
</wml>

You can see this script in action at: http://www.juicystudio.com/search.wml

You might also like...

Comments

About the author

Gez Lemon United Kingdom

I'm available for contract work. Please visit Juicify for details.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth