XHTML FML Shopping Cart Form

an article added by: Albert Lichtblau at 06022007


In: Categories » » HTML XHTML and CSS » XHTML FML Shopping Cart Form

The Shopping Cart Form You now know enough of XHTML-FML to create a contact information form, so it's time to move to more complex markup. You can take some of what you've learned so far to create an interactive, generic shopping cart complete with on-the-fly calculations, images, and cross-layer navigation – all with FML.

Start with a single product You're about to start a completely new segment of your e-commerce order form so you should save your contact information form (contactform.xhtml) and open a new file. Save this new file and call it

shoppingcart.xhtml. The first step is to set up a toggled <x:pulldown> menu with several <x:option> elements having numerical values of 0–6. The eighth <x:option> element should utilize the same onclick event handler as the preceding editable list used more as the option content (not value). The <x:textinput> element should strictly validate for a numerical value and should utilize the same onchange event handler as the previous editable list. Finally, you need to add the item the person is purchasing along with a brief description about it. This is your initial source code inside the body of a new XHTML-FML document:

   <x:form>
   <p>&nbsp;</p>
   <table  bgcolor="lightblue" style="border-color:blue"  border="2">
   <tr>
   <td colspan="2"  align="center">Key</td>
   </tr>
   <tr>
   <th>Dollars</th><th>Coins</th>
   </tr>
   <tr>
   <td>$.10</td><td>Copper  Pieces (CP)</td>
   </tr>
   <tr>
   <td>$1</td><td>Silver  Pieces (SP)</td>
   </tr>
   <tr>
   <td>$10</td><td>Gold  Pieces (GP)</td>
   </tr>
   <tr>
   <td>$100</td><td>Platinum  Pieces (PP)</td>
   </tr>
   </table>
   <p>&nbsp;</p>
   <table style="border-color:blue"  border="2" bgcolor="lightblue" cellpadding="10"  width="85%"
   align="center">
   <tr>
   <td>
   Fiery Avenger
   </td>
   <td>
   A powerful magic sword, <br /> the blade  is a red-hot flame
   </td>
   <td>
   <x:toggle id="editamount"  shared="yes">
   <x:pulldown id="amount"  send="yes">
   <x:option  value="0">0</x:option>
   <x:option  value="1">1</x:option>
   <x:option  value="2">2</x:option>
   <x:option  value="3">3</x:option>
   <x:option  value="4">4</x:option>
   <x:option  value="5">5</x:option>
   <x:option value="6"  onclick="toggle:editamount">More</x:option>
   </x:pulldown>
   <x:textinput size="5"  id="more" onchange="toggle:editamount"  ctype="num" validation="strict" />
   </x:toggle>
   </td>
   </tr>
   </table>
 </x:form>

Now that you have your product and the ability to choose an amount, you need to set a price and something to calculate a total. Enter two new elements: <x:textoutput> and <x:calc>. You use <x:textoutput> to set the price. It's a static amount, so there is no need for a user to modify it. Next you insert the <x:calc> element to add up the sum of the price and the amount. Note that <x:calc> does not actually output anything directly to the browser per se. The second <x:textoutput> wrapped by the <x:calc> inherits the value of total and prints it.

   <td>
   <x:textoutput id="price"  value="10000" send="yes" /> PP
   </td>
   <td>
   <x:calc id="total"  term="amount * price" send="yes">
   <x:textoutput />
   </x:calc> PP
   </td>
   Let's add a new item with a  different price appearance:
   <tr>
   <td>
   Honey Mead
   </td>
   <td>
   Made with the best Royal Jelly<br />
   (Wasp Honey)
   </td>
   <td>
   <x:toggle id="edit2amount"  shared="yes">
   <x:pulldown id="amount2"  send="yes">
   <x:option  value="0">0</x:option>
   <x:option  value="1">1</x:option>
   <x:option  value="2">2</x:option>
   <x:option  value="3">3</x:option>
   <x:option  value="4">4</x:option>
   <x:option  value="5">5</x:option>
   <x:option value="more"  onclick="toggle:edit2amount">More</x:option>
   </x:pulldown>
   <x:textinput size="5"  id="more2" onchange="toggle:edit2amount"  ctype="num" validation="strict" />
   </x:toggle>
   </td>
   <td>
   <x:textoutput id="price2"  value="1.60" send="yes" /> SP
   </td>
   <td>
   <x:calc id="total2"  term="amount2 * price2" send="yes">
   <x:textoutput />
   </x:calc> SP
   </td>
 </tr>

Notice how even though this mead costs 1 Silver Piece and 6 Copper Pieces, it keeps rounding up to a whole number. You easily can fix this problem by adding the digits element to the <x:calc> element.

   <x:calc id="total2"  term="amount2 * price2" send="yes" digits="2">

Giving a value of 2 to the digits element tells Mozquito to round up based on the first two digits after the decimal point. In order to show the example of decimal point rounding, you add a second product. You may not have noticed that the source code has nearly doubled in length from 80 to 112 lines. If you cut-and-paste, you may also miss the fact that not only do you have to change most of the values, but each id must be different as well. Annoying, isn't it? Think of how tedious it is if you have to add 100 items or more. Thankfully, there's a new element to come to the rescue called <x:template>. You simply define the entire table row as part of a template and then use the <x:insert> element any time you want to make use of your template – a much easier process than the original solution. First, you need to remove the table from the <x:form> element, place it inside <x:template>, and give the template element an id value of item. (Don't forget to add the </x:template> after the table closes.)

   <x:template id="item">
   (Item Table)
 </x:template>

Next, back in the <x:form> element, you add the <x:insert> . Each instance of this element gets its own id value and references the template with an attribute:

   <x:form>
   (Monetary Key Table)
   <x:insert id="eq1"  template="item">
   <x:insert id="eq2"  template="item">
 </x:form>

 

Here's what your FML looks like with it all put together. Note that you've temporarily removed the second item table altogether.

   <?xml version="1.0"?>
   <!DOCTYPE html PUBLIC  "-//OVERFLOW//DTD XHTML-FML 1.0//EN"
   "http://www.mozquito.org/dtd/xhtml-fml1.dtd">
   <html  xmlns="http://www.w3.org/1999/xhtml" xmlns:x="http://www.mozquito.org/xhtml-fml">
   <head>
   <title>Untitled</title>
   <meta name="generator"  content="Mozquito Factory 1.2" />
   </head>
   <body>
   <x:template id="item">
   <table style="border-color:blue"  border="2" bgcolor="lightblue" cellpadding="10"  width="85%"
   align="center">
   <tr>
   <td>
   Fiery Avenger
   </td>
   <td>
   A powerful magic sword, <br /> the blade  is a red-hot flame
   </td>
   <td>
   <x:toggle id="editamount"  shared="yes">
   <x:pulldown id="amount"  send="yes">
   <x:option  value="0">0</x:option>
   <x:option value="1">1</x:option>
   <x:option  value="2">2</x:option>
   <x:option  value="3">3</x:option>
   <x:option  value="4">4</x:option>
   <x:option  value="5">5</x:option>
   <x:option value="6"  onclick="toggle:editamount">More</x:option>
   </x:pulldown>
   <x:textinput size="5"  id="more" onchange="toggle:editamount"  ctype="num" validation="strict" />
   </x:toggle>
   </td>
   <td>
   <x:textoutput id="price"  value="10000" send="yes" /> PP
   </td>
   <td>
   <x:calc id="total"  term="amount * price" send="yes">
   <x:textoutput />
   </x:calc> PP
   </td>
   </tr>
   </table>
   </x:template>
   <x:form>
   <p>&nbsp;</p>
   <table bgcolor="lightblue"  style="border-color:blue" border="2">
   <tr>
   <td colspan="2"  align="center">Key</td>
   </tr>
   <tr>
   <th>Dollars</th><th>Coins</th>
   </tr>
   <tr>
   <td>$1</td><td>Copper Pieces  (CP)</td>
   </tr>
   <tr>
   <td>$10</td><td>Silver Pieces  (SP)</td>
   </tr>
   <tr>
   <td>$100</td><td>Gold Pieces  (GP)</td>
   </tr>
   <tr>
   <td>$1000</td><td>Platinum  Pieces (PP)</td>
   </tr>
   </table>
   <p>&nbsp;</p>
   <x:insert id="eq1"  template="item" />
   <x:insert id="eq2"  template="item" />
   </x:form>
   </body>
 </html>

This is great because instead of duplicating the huge table, you add only four new lines of code. But something's still missing. In order to make use of the template, you have to dump the table containing the Honey Mead item. How can you get that back in there while still making use of the code-reducing template element? Simple. You use the <x:prop> element. Every time you use the <x:insert> element, you can have a series of <x:prop> elements that feed the template the different data for each new item. In the template, replace the item property mentions with id values surrounded by pipe symbols (|). For example:

 <x:textoutput id="price"  value="10000" send="yes" /> PP

changes to

   <x:textoutput id="price"  value="|cost|" send="yes" /> PP

Under the <x:insert> element, add the price variables:

   <x:insert id="eq1"  template="item">
   <x:prop  name="cost">10000</x:prop>
   </x:insert>
   <x:insert id="eq2"  template="item">
   <x:prop  name="cost">1.60</x:prop>
 </x:insert>

Once you add in all the other fields and data, here's what the full FML form looks like:

   <x:template id="item">
   <table style="border-color:blue"  border="2" bgcolor="lightblue" cellpadding="10"  width="85%"
   align="center">
   <tr>
   <td>
   |item|
   </td>
   <td>
   |description|
   </td>
   <td>
   <x:toggle id="editamount" shared="yes">
   <x:pulldown id="amount"  send="yes">
   <x:option  value="0">0</x:option>
   <x:option  value="1">1</x:option>
   <x:option  value="2">2</x:option>
   <x:option  value="3">3</x:option>
   <x:option  value="4">4</x:option>
   <x:option  value="5">5</x:option>
   <x:option value="6"  onclick="toggle:editamount">More</x:option>
   </x:pulldown>
   <x:textinput size="5"  id="more" onchange="toggle:editamount"  ctype="num" validation="strict" />
   </x:toggle>
   </td>
   <td>
   <x:textoutput id="price"  value="|cost|" send="yes" /> PP
   </td>
   <td>
   <x:calc id="total"  term="amount * price" send="yes" digits="2">
   <x:textoutput />
   </x:calc> PP
   </td>
   </tr>
   </table>
   </x:template>
   <x:form>
   <p>&nbsp;</p>
   <table bgcolor="lightblue"  style="border-color:blue" border="2">
   <tr>
   <td colspan="2"  align="center">Key</td>
   </tr>
   <tr>
   <th>Dollars</th><th>Coins</th>
   </tr>
   <tr>
   <td>$1</td><td>Copper Pieces  (CP)</td>
   </tr>
   <tr>
   <td>$10</td><td>Silver Pieces  (SP)</td>
   </tr>
   <tr>
   <td>$100</td><td>Gold Pieces  (GP)</td>
   </tr>
   <tr>
   <td>$1000</td><td>Platinum  Pieces (PP)</td>
   </tr>
   </table>
   <p>&nbsp;</p>
   <x:insert id="eq1"  template="item">
   <x:prop  name="cost">10000.00</x:prop>
   <x:prop name="item">Fiery  Avenger</x:prop>
   <x:prop name="description">A  powerful magic sword with a burning flame for a blade</x:prop>
   </x:insert>
   <x:insert id="eq2"  template="item">
   <x:prop  name="cost">1.60</x:prop>
   <x:prop name="item">Honey  Mead</x:prop>
   <x:prop name="description">Made  with the best Royal Jelly (Wasp Honey)</x:prop>
   </x:insert>
 </x:form>

This is great, but it's still missing something. It would be convenient if you could also set up a running grand total of what you've purchased so far. This is quite simple. After the group of <x:insert> elements, set up another <x:calc> equation. This time, add the values of the total for each <x:insert> element:

   Total:
 <x:calc id="grandtotal"  term="eq1.total + eq2.total" digits="2">
 <x:textoutput />
 </x:calc>

Be sure to wrap your table around it:

   <table style="border-color:blue"  border="2" bgcolor="lightblue" cellpadding="10"  width="85%"
   align="center">
   <tr>
   <td colspan="5">
   <strong>Total:
   <x:calc id="grandtotal"  term="eq1.total + eq2.total" digits="2">
   <x:textoutput />
   </x:calc>
   </strong>
   </td>
   </tr>
   </table>
 

Layers Earlier I mentioned the convenience of templates, inserts, and props if you have a huge database of items that you want to add to your form. Even if you have only a few, it's still a convenient way to get the job done. Along the same lines, why list all your items on one page? You can take advantage of the <x:toggle> element that you've already used for editable lists to create multiple pages within one FML document. To do this, place an <x:toggle> element with an id attribute value of navigate just above your first <x:insert> element and below the <x:form> element. Place the corresponding </x:toggle> under the last </x:insert> element.

   <x:toggle  id="navigate">
   <x:insert id="eq1"  template="item">
   <x:prop  name="item">Fiery Avenger</x:prop>
   <x:prop  name="description">A powerful magic sword with a burning flame for  a blade</x:prop>
   <x:prop name="cost">10000.00</x:prop>
   </x:insert>
   <x:insert id="eq2"  template="item">
   <x:prop  name="item">Honey Mead</x:prop>
   <x:prop  name="description">Made with the best Royal Jelly (Wasp  Honey)</x:prop>
   <x:prop  name="cost">1.60</x:prop>
   </x:insert>
 </x:toggle>

Underneath the "grand total" table, place two <x:button> elements using the event handler onclick as follows:

   <x:button value="Back"  onclick="toggle:navigate,-" />
   <x:button value="Forward"  onclick="toggle:navigate,+" />

After you make these changes and additions, save your file and push it through to your browser.

Cleaning up Now that you've covered most of FML, you can finalize the shopping cart portion of your e-commerce order form by adding some images. Using the <x:prop> element, you can preload images for each item. As an example, modify the location of the |item| variable and add a new element <x:img>:

   <td>
   |item|
   </td>
   to
   <td>
   |item| &nbsp;<br  /><x:img src="|image|" alt="|item|"  width="50" height="50" preload="yes" />
 </td>

Note the reuse of the |item| variable for the value of the alt attribute. Also, for <x:img> elements, it is a requirement to set the width and height. In order to prevent long load times as you move from item to item, add an attribute preload set to the value of yes. This loads all the <x:prop>-set images at once. Don't forget to add a <x:prop> element for |image|:

   <x:toggle id="navigate">
   <x:insert id="eq1"  template="item">
   <x:prop  name="cost">10000.00</x:prop>
   <x:prop name="item">Fiery  Avenger</x:prop>
   <x:prop name="description">A  powerful magic sword with a burning flame for a blade</x:prop>
   <x:prop  name="image">http://www.eqmaps.com/itemicons/2hslash003.jpg</x:prop>
   </x:insert>
   <x:insert id="eq2"  template="item">
   <x:prop  name="cost">1.60</x:prop>
   <x:prop name="item">Honey  Mead</x:prop>
   <x:prop name="description">Made  with the best Royal Jelly (Wasp Honey)</x:prop>
   <x:prop  name="image">http://www.eqmaps.com/itemicons/potion001.jpg</x:prop>
   </x:insert>
   </x:toggle>
 

legal notice

Our website is not responsible for the information contained by this article. Web-articles is a free articles resource.
Suggestion: If you need fresh, daily updated content for your website, feel free to use our service. Click here for more information.

Useful tools and features

Link to this article from your page    Send this article to you or to a friend
If you like this article (tutorial), please link to it from your web page using the information above.

related articles

1. HTML and XHTML Application Possibilities
Overview Shifting from HTML to XHTML requires a significant change in mindset from the design-oriented freefor- all that characterized the early years of the Web. This change in style reflects movement in the underlying architecture toward a more powerful and more controllable approach to document creation, presentation, and management. Understanding the connections between the architectural and stylistic changes may help you find more immediate benefits from XHTML –...

2. Coding Styles HTMLs Maximum Flexibility
The XHTML 1.0 specification provides a set of rules for XHTML (User Agent Conformance) that includes a rough description of how XHTML software differs from HTML software, though these rules exist mostly to bring XHTML rendering practice in line with the rules for parsing XML 1.0. XHTML also is designed to remain compatible (mostly) with the previous generation of HTML applications, so it may take a while for the transition to occur. Pure XHTML user agents (also known as XHTML processing software) aren't l...

3. XML and XHTMLs Maximum Structure
Coding Styles— XML and XHTML's Maximum Structure Overview XML parsers are far more brutal about rejecting documents they don't like than are HTML browsers. XML's clear focus on structure demands that the practices described in the previous chapter must change. However, most of those changes shouldn't cause more than minor inconveniences – at least for newly created documents. Note If reading this chapt...

4. XML and CDATA
Processing instructions XML also enables developers to pass information to the application through processing instructions (often called PIs). Processing instructions use a similar syntax to the XML declaration, although the rules for them are much less strict. Processing instructions begin with <? and end with ?>, but the developer generally dictates their contents. The first bit of text before a space appears in a PI is called the target. The target must start with a letter, unde...

5. lang Internationalization
Internationalization: xml:lang and lang Internationalization (often abbreviated i18n because 18 characters appear between the i and the n) gets a significant boost with the shift to XML primarily because of XML's use of Unicode as the underlying character model. While not every document needs to encode Chinese, Cyrillic, Arabic, and Indian characters, Unicode makes it possible for all of these forms to exist within a single document. In addition, XML and XHTML allow for the possibility of other e...

6. Anatomy of an XHTML Document
The transition from HTML to XHTML will come with a fair number of bumps. While later chapters introduce tools to help you get past those bumps – and figure out where they come from – this chapter examines what's going to change and demonstrates a few strategies for handling those changes. Along the way, we visit the ghosts of browsers past and explore problems that exist in current browsers. In turn, you discover how prepared and unprepared various tools are for XHTML. Note Som...

7. Converting to strict HTML and XHTML
Converting to strict HTML You start out by declaring your intentions to use the strict HTML 4.01 DTD by putting the appropriate DOCTYPE declaration at the head of the document: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> Now the first section of the document, including the HTML opening tag and the HEAD element and its contents, is fine except for one line. The SCRIPT element no longer supports a LANGUAGE at...

8. Reading the XHTML DTDs A Guide to XML Declarations
Reading the XHTML DTDs: A Guide to XML Declarations Although the W3C has long had document type definitions (DTDs) for HTML, few developers actually use those DTDs as a foundation for learning HTML. XHTML 1.0 simplifies those DTDs with the slightly friendlier XML syntax – they previously used SGML's more complex syntax – and the increased emphasis on validation may lead developers to explore them more closely. Making good use of XHTML 1.1 requires some level of ...

9. Defaulting attribute values XHTML DTDs
XML 1.0 also provides a set of tools for specifying what happens if an attribute isn't declared within an element. Four different possibilities exist, including "the attribute just isn't there"; "the attribute must be there, period"; and "the attribute has this value, period." You already have seen a few uses of these choices in the preceding declarations. In the img element, for instance, the src and alt attributes are required (#REQUIRED); meanwhile, most of the rest of its attribute content is optio...

10. Exploring the XHTML DTDs
Exploring the XHTML DTDs Choosing Your DTD XHTML 1.0 provides three DTDs that describe different sets of XHTML elements and reflect the three choices provided in HTML 4.0: strict, transitional, and frameset. The probably the one that the W3C would like to see developers adhere to, but transitional DTDs reflect the reality of HTML usage much more accurately. Appendix A lists the in the three different DTDs, along with notes regarding attributes. To identify the DTD for a ...