Support

Home Forums Event Espresso Premium How does State/Province populate based on Country?

How does State/Province populate based on Country?

Posted: February 28, 2022 at 2:29 pm

Viewing 2 reply threads


PTOee4

February 28, 2022 at 2:29 pm

Hello,
I have set up my EE event registration form such that the ‘State/Province’ field immediately follows the ‘Country’ field. It successfully populates only with the appropriate values per the chosen country. Cool!

My question: how is this done, and can I use this functionality elsewhere?

Background: I use Ultimate Member for user registration and Advanced Custom Fields to save a few extra fields to wp users (including Country and State/Province). I would like to implement Country and State/Province dropdowns elsewhere that mimic the same cool behavior as used by the EE event reg form. For example. (I’m aware this is way outside the scope of EE, but still curious how EE does it, and if usable elsewhere).

It looks like countries and states/provinces are in database tables? esp_country, esp_state ?

Cheers, and thanks!


Tony

  • Support Staff

March 1, 2022 at 10:56 am

Hi there,

First, thanks for noticing the feature (nope, it’s not one of mine) 🙂

That feature is actually pretty simple, the front end simply filters the state value using some JavaScript to set the optgroup (and also each individual option within the dropdown) elements for every other countries state to have a style of display: none;

So for example if you select the United Kingdom, the ‘states’ field (which are actually counties over this way) would look something like this:

https://monosnap.com/file/PEBRDL42jssxR4tddtXUQxfKD0pvXF

Notice how every other ‘optgroup’ has display: none; set on them but not UK.

Thats basically it…. lol it happens HERE.

Now, one of the reasons we are able to do that is because of how we handle countries and states internally. All states are linked to the country so when we generate the state output we can separate each country’s states into their own optgroup and make this possible.

Now…. ‘could’ you pull that functionality out of Event Espresso and use it elsewhere OR even use the EE models to output those fields somewhere else completely, sure! It might be a little fragile as it’s dependent on how we set up our countries in the DB though.


PTOee4

March 1, 2022 at 12:26 pm

Thx!

Viewing 2 reply threads

The support post ‘How does State/Province populate based on Country?’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso