Selectbox based on values, but not using DB


#1

Hi
I’ll try to explain this the best way I can. I have a countrylist (multilingual, and works fine), and I would like to see another one being populated based on the first selection. However, I don’t really need to get that second part from a DB since those values will stay the same. I have tried it with javascript but that did’t really work. The code for the first selectbox is:

<?php $options = array('' => 'Alle Landen', 'Germany' => $_LANG['C_GERMANY'], 'Andorra' => $_LANG['C_ANDORRA'], 'England' => $_LANG['C_ENGLAND'], 'Austria' => $_LANG['C_AUSTRIA'], 'Belgium' => $_LANG['C_BELGIUM'], 'Scotland' => $_LANG['C_SCOTLAND'], 'Spain' => $_LANG['C_SPAIN'], 'France' => $_LANG['C_FRANCE'], 'Greece' => $_LANG['C_GREECE'], 'Canary' => $_LANG['C_CANARY'], 'Ireland' => $_LANG['C_IRELAND'], 'NorthIreland' => $_LANG['C_NIRELAND'], 'Italy' => $_LANG['C_ITALY'], 'Madeira' => $_LANG['C_MADEIRA'], 'Malta' => $_LANG['C_MALTA'], 'Netherlands' => $_LANG['C_NETHERLANDS'], 'Wales' => $_LANG['C_WALES'], 'Portugal' => $_LANG['C_PORTUGAL'], 'Switzerland' => $_LANG['C_SWITZERLAND'], 'Bulgaria' => $_LANG['C_BULGARIA'], 'Croatia' => $_LANG['C_CROATIA'], 'Czech' => $_LANG['C_CZECH']);?> <div align="center">LAND:<br> <select name="search_country" class="style1"> <?php foreach ( $options as $key => $value) { $selected = ''; if(isset($_GET['search_country']) && $_GET['search_country'] == $key) { $selected = ' selected '; } echo '<option value="' . $key . '" ' . $selected .'>' . $value . '</option>'; } ?>

So basically, if somone selects Netherlands it should display the provinces of the netherlands from a preset [hardcoded] list. Everything I tried so far results in either no list at all, code errors or a full list with every province of every country. Can I add an ‘if’ statement somehow that checks value in box one and based on that display anything that matches the if statement? If so, any pointers in where to start (besides the manual)?
What is the best way to achieve the above (and please remember I’m a beginner).

thanks once again!


#2

I think what you need is a page refresh:

If no country code is set, we’re on the first page, and a list of countries should be displayed (all countries from the DB).

If a country code is set, we should display the ‘second’ page, which lists only the states or provinces from the country selected on the first page. This can be done with a simple WHERE clause in a SQL query.

The hardcoded list is pretty much a no-go. I wouldn’t go for that. Nor for the Javascript idea, as it’s very easy for visitors to turn off Javascript execution in their browsers (rendering your script crippled).


#3

thanks

so what you suggest is that the provinces are stored in a db (as a subcat of country).

On first visit it’ll show an unselected country and therefore no provinces selectbox will be shown. After the first query is done (country) I should do a new db search for the provinces, where country == selectedcountry.

Correct?

I’ll give it a go!

The reason I used the javascript and hardcoded the countrylist is that I want the selectbox to be multilingual (names change, value stays the same), and I wanted to make sure that no matter what page I click after a search the values stay selected (home -> searchresults -> details _. more details etc). I don’t know how to do that in PHP without messing up my code once again…

And the hardcoded part for the provinces wan’t too big a deal since it’s very unlikely that the countries in my list will add or remove privinces. But the turning off of javascript is!

thanks (again)
JP