Dynamic Forms

4 replies [Last post]
Last seen: 10 years 19 weeks ago
Joined: 2011-03-13

I was wondering how you would apply the #ajax attribute to multiple form elements within the same scope of the video tutorial but dependent on one another and specifically query the database for those values instead of hardcoding them?
So as an example:
Lets say someone is selecting an ingredient for a recipe with fields like:
1.Food group category (food_group_table)
2. Ingredient name (ingredient_table)
3. Serving Size (ingredient_weight_table)
So for example someone selects from a dropdown the category of dairy > an ingredients dropdown is created based on those only for that category ie; milk, eggs, etc... > a serving size dropdown is displayed showing only serving sizes related to that ingredient ie; 1 cup, 1 ounce, 1 pound, etc...
Hope that makes sense. I guess i'm wondering if there is something special that would have to be done to check the form state of both 1 and 2 when displaying field 3 or is field 2 only necessary at that point?

I've seen similar questions in the drupal forums with no really good "how to" answers so a followup video to the ajax forms video utilizing this example would be awesome. Chris, if you could come up with something like this, i'd really like to see the code to query the database as well instead of just hardcoded arrays so the lesson learned is more real world and applicable. If anyone else has insight on how to accomplish this, i'd love to hear some feedback.

P.S. The site is awesome, much needed and worth every penny! I hope it continues to be successful for you... and us!

Chris Shattuck
Last seen: 10 weeks 4 days ago
Joined: 2009-08-16
Hello apmsooner! First of

Hello apmsooner!

First of all, thank you for the kind words, I'm very happy you're enjoying the site!

Second, I think doing a follow up on the Ajax videos is definitely in order. As you say, there have been multiple questions around this. It may be a little while, but I'll post back to this thread when they're released.

In terms of the database examples, I'd love to hear if you have anything specific in mind. Do you find the videos on the Database API need some clarification or extension, or is it difficult to see how you would connect to the Database API when processing the Ajax side of a form? I just want to know if I glossed over anything that I should give a bit more attention. :)

I like the idea of adding some more complex examples that tap into several APIs now that there's a solid base of more abstracted videos, and combining form building and the Database API seem like a good place to start.

Thanks for the feedback!

Last seen: 10 years 19 weeks ago
Joined: 2011-03-13
"..or is it difficult to see

"..or is it difficult to see how you would connect to the Database API when processing the Ajax side of a form?"
Hi Chris, i think the above statement is what i could use some clarification but perhaps connecting to the database API in general would be helpful to have some more real world examples. I feel when learning anything new, its helpful to relate the content being learned to how i could specifically apply it to a real project. On top of that, I have questions...perhaps some clarity on when to create tables as nodes and when not? Reason i ask this is i've read different opinions about performance issues with cck vs. generic tables (something to do with more queries involved??). I realize the value in cck is the ability to easily extend it by adding fields through a UI but is there a basis for not tapping into the node api when dealing with larger amounts of data? I notice several modules create their data not using the node api so wondering if a developer tends to choose this route for performance reasons or other?
I've definitely researched the api.drupal.org pages and while the references seem to be fairly complete, the majority of examples to me at least seem to be pretty vague when trying to apply the logic to my own projects. Thanks!

Chris Shattuck
Last seen: 10 weeks 4 days ago
Joined: 2009-08-16
Great questions,

Great questions, apmsooner.

There are a couple reasons why developers store data in their own tables rather than in nodes. One is performance. If you follow what happens with node_load(), you'll see that there's a lot of hooks called, quite a few database queries, and some extraneous data being stored in the $node object that might not always be useful.

In terms of querying large amounts of data, I haven't seen benchmarks, but the number of JOIN's required, especially in Drupal 7, will take more resources than querying a flat database. But there are some modules that can help with this if it becomes an issue, by creating a table-based cache.

The other is that for many developers, it's just easier to work with your own tables. You get to set the rules, the table structure and make queries directly instead of through an API. This has a design debt, though. You don't get to leverage all the awesome APIs, and if you anticipate other developers wanting to plug in, you can give them a head start by using the existing APIs.

Practically speaking, it seems like most projects are small enough that some performance inefficiencies with queries aren't a super big deal, so it's more of an opportunity to figure out the benefits of each approach before you hit a really big project where those little things matter.

Great thoughts on providing some more practical examples of the Database API, I'll definitely keep this in mind for future videos.


Last seen: 2 years 15 weeks ago
Joined: 2011-07-12
working with the initial example: food groups

**chris, -(as if you didn't know, but...) - you have my permission to edit/use this comment, in short or entirety, as you deem most fit. this is my very first posting of this sort, and i'm quite afraid that i will not explain my ideas very well in grammar, syntax, or logic. here goes...

/*i look forward to any useful criticisms and ideas that come to the resolution of this example. i will revisit the coding standards video soon (i promise.); this work is only intended to convey the conceptual relationship of preloaded default settings for users, conditionally loaded dynamic selectors, and the functionality of a saving process that attaches the current form selectors information to a file. it is my goal to develop an online dynamic ui in one form.


i, too, have found buildamodule.com to be one of the best drupal resources available. it's been the most coherent learning platform, i've used. it's foundational concepts have been the glue for all the time spent on user submit vids , the api docs, and user forums. omg,thank you.

example: "food group selector form"

- when the form validation reads true, the default selectors are preloaded (=> preload_(?)) with a dynamic ajax function*
/* is this a get_ / _set issue?

- the ("food group") = selector field, loads (default) => (default= "vegetables_#uid"), => trigger #ajax load= (default= ('fresh vegetables_#uid')

/* for my client, i need to have permissions accessible (why i added uid) dynamic form selectors that load in 'collapsed' style. i suppose the information table will have to be saved somewhere... i am exploring session variable and managed/unmanaged files as only auth users will have permission to "save".

(, "it is preset (default - 'food group') to something "healthy"... like 'vegetables'.) this setting should trigger the corresponding default selectors, or, renew selectors when changed by user.

#preload_'fresh vegetables' for that food group (limited variable terms). these terms: ie- "carrots, broccoli, squash..." in turn, trigger another dynamic selector, that maybe has 'portions' or 'recipes' as selector terms.

so, each selector menu is conditionally determined by the preceding selector variable from the previous menu (menu weighted?).

i think that this is explained in d7 core concepts #8...
also see from the example.module: ...sites/example.com/?q=examples/ajax_example/dependent_dropdown

suggestions on a more productive way to outline/brainstorm are welcome. if anyone is interested, i am willing to learn from/ collaborate with someone who is interested in extending functionality along these ideas.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:

Contact us

Below are the supported keyboard shortcuts for our video player.

Start / stop video
Go to next video
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
Jump to a percentage (2 is 20%, for example)
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
1 month
6 months
10% off
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.