Jump to content

select province/state for one country only


lional

Recommended Posts

Hi

I would like to write a script where it lists all the countries of the venues in a drop down box.

If south africa is selected it then shows the provinces in a second drop down box.

If any other country is selected, then no province/state drop-down box is shown

 

Thanks in advance

 

Lional

Link to comment
Share on other sites

What you are asking about is really a JavaScript question and not a PHP question (unless you want to have the user submit the page whenever they select a country).

 

You have several options on how to implement this and the "best" solution for your situation will depend on a few factors.

 

1. If you have a somewhat limited list of countries and provinces, I would gather all the data and write it out as JavaScript arrays on the page, then handle all the functionality client-side. How many is really personal decision. But, this solution will give the best possible performance.

 

2. Another option is to utilize AJAX (JavaScript + PHP). The JavaScript code would handle all the same logic as above (detecting a change in country, repopulating the list, etc.) But in this situation the JavaScript would make a "silent" call back to a PHP script to get the list of new provinces to populate whenever the user changes the country. So, there would be a slight performance hit for this. But, with this solution you don't need to download all the Countries/Provinces to the user's PC.

 

3. As stated originally, you could require the user to submit the page every time they change the country. NOt a very good solution, but you probably need to allow for this with any of the other solutions unless you want your page to be unusable by those without JS enables

 

4. There is one other solution I can think of. I once built a photo gallery that was completely run by JS. You can use a script tag to include an external JS file. Then dynamically change the source of the file (in this case the list of provinces) on the fly. That was many years ago and I don't know if it would be fully supported in all current browsers.

 

[Moving to JavaScript forum]

Link to comment
Share on other sites

Well, baaselect is a custom class - it is not part of the PHP core code. The class has documentation on how to set it up. The only thing that class is for is creating linked select lists, so I don't know how you were using it before if you didn't know how to set it up.

 

If you only want options available for South Africa, then don't have any options in the database for the other countries.

 

But, really, this is overkill for what you need. Simply create two select lists, one for countries and one for State/Province that has all the values for the South Africa selection. Then add a simple onchange event to the country select list to display/hide the state/province field based on the country value.

Link to comment
Share on other sites

Hi

I don't develop much in javascript so can someone point me in the right direction.

I would like a select list that pulls the countries from a mysql table. I only want to list the countries that I have venues for.

Then if South africa is selected, I want to pull the south african provinces from the database, again I only want to list the south african provinces hat I have venues for.

I have a countries table and a provinces table. I write the country to the countries table as I add a new venue if the country does not already exist in the table, and the same for the provinces table.

 

Thanks in advance

 

Lional

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.