4

I've got three custom dropdown fields - town, county, and region in a SafeCracker form. The client is asking if it's possible for them to just select one value (in the town dropdown field), and for the other two to pre-populate with the correct county and region. I do have a spreadsheet of town names that has the region and county for each town.

I've searched for add-ons that might do this but haven't managed to find any, so just wondering if there are any recommendations on how to go about doing this in a SafeCracker form? I'm not even sure what fields I'm best using, or whether this needs to be done outside of EE completely? Thanks in advance for any advice on this.

2 Answers 2

4

Typically you'll do this sort of thing by writing some jQuery code and storing that in the Instructions field of one of your field's definitions. That field supports HTML, so anything inside a <script/> tag will run when the page runs.

You can then set up an onchange event handler that updates the other fields when the primary field is changed.

To make this flexible, I'd recommend creating channels for your towns, regions and counties. That way they can be maintained by the client. You can use AJAX to query the database (using a custom template as the AJAX URL), so that when a town is selected, it finds the corresponding county & region in the database, and updates the corresponding fields accordingly.

0
3

Since you only want to let your visitors choose the town, and you already know which region and county each town belongs to, why not make the "towns" field a Relationship or Playa field, and relate it to a "Towns" channel, which has the Region and County data stored there as channel fields?

The user never needs to see this additional (Region, County) info on the front-end (so no javascript or AJAX required), but the data is there for use in your templates and by your client.

2
  • This option makes a lot of sense too, if the client doesn't need access to override those values, and you could still make those relationship fields to channels where you maintain their values. Feb 13, 2013 at 16:55
  • Thanks for this suggestion. I like the idea of using Playa (especially if it means I don't have to use JS/AJAX). My main show-stopper to going this route is that elsewhere on the site, I'll need to output entries based on town/county/region. I'd be doing this in a channel entries loop where the search parameter will be filtering the output by segment_3 (which will be a town/county/region) of the URL. Not sure I'd be able to filter entries in this way if these were stored in Playa. But I'll think about it some more.
    – Stephen
    Feb 13, 2013 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.