Forums

Ajax enabled dropdown

HerKle 22 Jul, 2013
Good Monday!

I have a form which loads data records via multi-record load. When it finds only one record, it is easy to preload those data into the form.

When it finds more than one record, I want to display the key values in a dropdown select. And when selecting, the form shall be preloaded with the data of the selected key value.

1. Question: can this be made by "ajax enabled dropdown"?

2. Does this require any submit button? As I understood the posts and faq, Ajax solves this dynamically "on-the-fly" just by selecting?

3. If not, do I need to create a second form or can I use the one I am already in?

Before getting deeper and learning JS, I would like to know if it's worth spending the time… ;-)

Thanks for your answers.
HerKle 23 Jul, 2013
No one who can answer my questions?
GreyHead 23 Jul, 2013
Hi HerKle,

The most common use of Ajax in forms is the 'double drop-down' where the choices in one select drop-down element set the options in another; so for example, picking a country in the first changes the list of counties/states/provinces in the second.

Please see this FAQ describes how to do that.

It is quite possible to extend that technique to load several (or many) different values. For example you could load the size, capital city, population, . . . of the country. Doing this will require some coding knowledge.

The alternative approach, which is simpler, is to use a multi-page form. Pick the country on the first page, then use the submitted value to load the data you need on the second page. In this case it can all be done without any JavaScript or custom code.

Bob
HerKle 24 Jul, 2013
Thanks Bob,

I will try that multi-page form approach.

In my special case I will have to create custom coded submit buttons for the next page? Since - as I wrote earlier - in case I have only one record loaded, I won't need any second page.

Herb
GreyHead 24 Jul, 2013
Hi Herb,

It is possible to write it so that the first page doesn't show if there is only one value (or perhaps automatically submits). That may be a bit advanced for you now, I'd try building the two page version to start with.

Bob
HerKle 24 Jul, 2013
Hi Bob,

to avoid meaningless buttons I solved it now by this:

1. Multi-record load data
2. Check if there are more than one record.
2.1 the form continues filling its fields with the preloaded data, if it's only one record
2.2 Event switch (GH), if there are more than one record, leading to custom code displaying the found records, with a link for each record ("Need to edit this?"). Link calls the form again, and since the check finds only one record, the form is filled with preloaded data.

I'm quite happy to got that solved without JavaScript - assisted by your ideas, Bob. Thanks again!
maspegren 02 Dec, 2013
Hello GreyHead, I have a similar question. I would like to have two dropdowns, where the second dropdown options are based on the choice of the first dropdown. My first dropdown is a custom Element pulling a list of Countries (for example). I have an SQL statement pulling from a database table in the Joomla database to list these countries in a dropdown. I would like my second dropdown to be similar, but the SQL statement will end like this: "...WHERE country = countryChosenFromFirstDropdown". I tried to use "WHERE country = '" . $form->data['firstDropdownName'] . "'" but this is still returning an empty set. Any ideas? I found that syntax here: http://www.chronoengine.com/faqs/63-cfv4/cfv4-working-with-form-data/2594-how-can-i-use-the-form-data-with-php.html. My second dropdown is also a Custom Element. I figured it would be easiest to Custom Elements since I have very indepth SQL statements. Please let me know if you need anymore information.
This topic is locked and no more replies can be posted.