My.ADVISOR.com Sign-In
Username
Password
Sign Up 
Go to Article
Advanced Search 

WEB DEVELOPMENT

Create Conditional Value Lists with FileMaker.php and AJAX

Make your FileMaker/PHP Web application shine with AJAX.

 DOWNLOAD (75,104 bytes) -- AnimalsDB.fp7 conditional value list example.
By Jonathan Stark, Jonathan Stark Consulting

UNLOCKED -- This article is provided to subscribers of FILEMAKER ADVISOR or DATABASED ADVISOR or FILEMAKER ADVISOR. To subscribe or renew, go to Advisor Store.

I recently received the following question:

Figure 1: To review -- Conditional value lists are a snap to build in FileMaker Pro, but ...


Figure 2: Web view -- ... getting conditional value lists to work on the Web is a bit trickier.

"I have a FileMaker Pro database I use to track animals. It has a form that has two pop-up value lists: Habitat and Type. If the Habitat pop-up has the value Land selected, then the values displayed in Type pop-up should include Lion, Tiger, Cheetah. Alternatively, if Habitat pop-up contains the value Sea, I want Shark, Dolphin, Whale to display. My aim is to try and recreate this FileMaker Pro behavior on the Web without using the Submit button to update the options in the Type pop-up."


As you might know, it's pretty easy to do this sort of thing in FileMaker Pro. Because FileMaker maintains a live connection between FileMaker Pro and FileMaker Server, you just select a Habitat and the Type value list options update instantly. Not so on the Web. The connection between your Web browser and the Web server isn't live. (The cool kids call this a "stateless" connection.) So, while you're selecting options in the Habitat pop-up in your browser, the Web server -- and by extension, FileMaker Pro -- doesn't know anything about it.

This statelessness leads to a predicament. The obvious course of action is to have the user submit the page after the first selection. However, that's an unintuitive and clunky thing to inflict on unsuspecting users, especially considering there could be more than two interdependent value lists involved. Select, submit, wait, select, submit wait ... users expect more elegance than that.

Enter JavaScript: A scripting language you can embed in HTML pages to perform actions when certain things occur. For example, you can put some JavaScript in your Web page and set it to execute when a user hovers the mouse pointer over a menu, or when she clicks on the Submit button on a form. When your JavaScript runs, it can do all sorts of things, such as display an alert dialog, open a new browser window, or validate a form.

In this article, I show you two ways to build conditional value lists on the Web using JavaScript. The first example isn't elegant, but works well in most cases. The second is much more sophisticated and extensible but is a little daunting at first. Both examples use the same FileMaker Pro database.

NOTE: I assume for the sake of discussion that you know how to build a conditional value list in FileMaker Pro (figure 1). If you don't, review the AnimalsDB.fp7 file (available for subscriber download).

Value lists reloaded

In this first example, I'll show you how to write some JavaScript that submits the page to the server whenever the user selects a new value in the Habitat pop-up.

Here are the steps the process takes:

  1. The user loads the page.
  2. She selects "Sea" from the habitat pop-up.
  3. JavaScript code automatically submits the form to the server with the selected habitat.
  4. The server checks the database for Type values related to the Sea habitat.
  5. The page is reloaded in the browser, but this time with the appropriate Type values filled in.

This entire example is contained in a single file named create_animal.php. Here's the code:

Open a php block and define your database connection information:

    <?php
    define('FM_HOST', '127.0.0.1');
    define('FM_FILE', 'AnimalDB');
    define('FM_USER', 'esmith');
    define('FM_PASS', 'm4rg0t');

Tip
For security reasons, include the connection info from a file located above the Web directory.

Create Conditional Value Lists with FileMaker.php and AJAX

No reader comments ... yet.

    What do YOU think about this topic? Share your advice and thoughts using this form.

    Your Name

    REQUIRED : PUBLIC

    Your E-Mail

    REQUIRED : PRIVATE

    Job, Company

    OPTIONAL : PUBLIC

    City, State, Country

    OPTIONAL : PUBLIC

    Your Web Site

    OPTIONAL : PUBLIC

    Your Comment

    Please help everyone by keeping your comments on-topic, using clean language, and not defaming or making personal attacks.


    Your e-mail address is required, but it will not be displayed to the public or given to anyone. See our Privacy Policy. Comments become visible after they pass our spam filter, and spammers and abusers are permanently blocked. Please report spam or abuse.

    Jonathan StarkJonathan Stark is an author and instructor who specializes in FileMaker/PHP Web applications for creative professionals. Past clients include Staples, Turner Broadcasting, and Ambrosi (now Schawk). He has spoken at the FileMaker Developers Conference, has had numerous articles published in FileMaker Advisor and php|architect magazines, and is the author of the book Web Publishing with PHP and FileMaker 9 available from SAMS Publishing. Jonathan is reluctant to admit that he began his programming career more than 20 years ago on a Tandy TRS-80. http://jonathanstark.com

    Printer-friendly
    page layout

    Keyword Tags: FileMaker, FileMaker Development, FileMaker FileMaker Pro, PHP, PHP language, Web Development

    ADVISORAMA
    You've got to stand for somethin' or you're gonna fall for anything.
    -- John Cougar Mellencamp, You've Got to Stand for Somethin'

    ARTICLE INFO

    FileMaker Advisor

    Web Edition: 2008 Week 18, Doc #19445

    Print Edition: June/July 2008, Page 10

    SUBSCRIBER ONLY ARTICLE LOCKED


    File: AnimalsDB.fp7 conditional value list example.
    DOWNLOAD: 75,104 bytes

    Subscribe to FileMaker Advisor Magazine

    Read the advanced guide to creating custom business database solutions with FileMaker software. Subscribe now to gain access to all the archives and downloads.

    FileMaker.Advisor.com

    Subscribe to Advisor Basics of FileMaker Pro

    Learn the fundamentals of using FileMaker Pro software. Every issue gives you step-by-step instructions on creating the databases you need. Subscribe now!

    FileMaker.AdvisorBasics.com

    Secrets Of The Top Experts -- Now!

    See exactly how to do it, step-by-step, in Advisor Academy CDs created by the top experts. Click to see what you can learn right now.

    AdvisorAcademy.com

    Free E-Newsletters

    Keep up! Hot News, How-To, Tips & Tricks, Expert Advice, and more. Click to request your's free.

    AdvisorUpdate.info

    Need Know-How Now?

    What direction are you going with your business? Advisor Guides are packed with the answers you need to work smarter. Can you afford to fall behind?

    AdvisorStore.com

    Showcase Your Smarts

    Submit your tips, techniques and advice and let Advisor promote your business and build your career. Show the world what you know!

    AdvisorTips.com

    Use of this or any other site, content, product or service of Advisor Media constitutes acceptance of Terms of Use.
    Portions copyright ©1983-2008 Advisor Media, Inc. All Rights Reserved.
    Reuse or reproduction of any portion or quantity of Advisor Media's copyrighted content, in any form, for any purpose, requires written permission.
    ADVISOR®, the ADVISOR logo, and other names and logos that incorporate ADVISOR are registered trademarks, trademarks or service marks of Advisor Media, Inc. in the United States and/or other countries.
    Other trademarks are used for identification, editorial or descriptive purposes and are the property of their owners.
    stark014 posted 04/28/2008 modified 05/12/2008 03:44:56 AM ztfmfd/ztfmfd
    domino-144.advisor.com my.advisor.com 05/16/2008 12:17:31 PM