Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Dynamic WML
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rdrake




PostPosted: Tue Nov 15, 2005 7:08 pm   Post subject: Dynamic WML

So you've read my last tutorial and you want to know, how can I make my pages dynamic? Seeing a page you've created is nice, but what if you could find information from a database? In this tutorial I'll show you how you can acheive this and more.

Most importantly, you must insert a header to show the WAP browser that the page is indeed WML. This is accomplished (in PHP) with the following statement.
code:
<?php
header("Content-type: text/vnd.wap.wml");
?>
If you're using Ruby or another CGI friendly language, just use something like so.
code:
puts "Content-type: text/vnd.wap.wml"
This tells the browser that it is WML that it'll be receiving, and not text/html like what's normally sent when dealing with HTML pages.

Now, all we have to do is make sure whatever we output follows the WML syntax, and we'll be fine.

Let's say we want to have a form on one page which links to a processing page. We would, of course have to build the page. It would look something like the following.
code:
<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>
  <card>
    <p>Name: <input name="name" size="15"/><br/>
      Age: <input name="age" size="15"/><br/>
      <anchor>OK
        <go href="process.php" method="post">
          <postfield name="name" value="$name"/>
          <postfield name="age" value="$age"/>
        </go>
      </anchor>
    </p>
  </card>
</wml>
The above code is a bit more complex than the code found in the previous tutorial, but don't worry, I'll explain it.

Like before, we have two input fields. We also have a link, with the text 'OK' which will take us to 'process.php'. It will use the POST method for passing the information.

The most important part of the above code is the postfield tag. It passes along the values of the input fields via the POST method and gives them the value $<whatever>. This is the name of the variable we will use to access this information in the next page.

Now we will process the code shown above.

Our page looks pretty similar at first glance, but it does get quite different. We're using PHP/Ruby to output the examples, so we must output the header.
code:
<?php header("Content-type: text/vnd.wap.wml"); ?>

code:
puts "Content-type: text/vnd.wap.wml"


We can now put the next part in plain text (if using PHP, with Ruby you must use puts). While we're at it, why don't we display the value of the variables?

code:
<?xml version=\"1.0\"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">')

<wml>
  <card>
    <? echo "<p>Your name is $name and you are $age years old.</p>"; ?>
  </card>
</wml>
Still not very dynamic, but remember, we can do anything we want to it. We could perform checks on the values, or repeat them as many times we want. Just remember, cell phones have very small bandwidth and your visitors are paying for it, so don't go too overboard. The page is outputted to WML after PHP is done with it, so whatever PHP can do is what you can display in a device that uses WML.

You can use the above with pretty much and web scripting language, or CGI language. Examples include PHP, Ruby, ASP (if you really must), Pike, PERL, and others.

Some applications include fetching information from a database based on user input and displaying it, or even checking flight information or showtimes. I've used it to allow people to search for what time their hockey game is.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: