Click to See Complete Forum and Search --> : list cities in drop down menu


themoon
10-22-2007, 03:10 AM
HI,
I have 2 mysql tables, namely country, city
country:
id, cont_name, is_enabled

city:
id, city_name, country_id

I have done a php function which pull the list of countries in from the db
and i output in the select option this way


<select name="countryid">
<option value="0" selected="selected">Select Country</option>
<?php get_all_countries(); ?>
</select>

Now, how can list all of the cities upon selecting the country from the drop down menu without refreshing the page. That's the concept of ajax.
& i want if the city is not available, a text box should be enabled for the user to type in the city which is not available in the drop down menu.

Thanks in advance.

cridley
10-22-2007, 07:09 AM
have the onchange event of the country dropdown call a function which calls a php page via ajax (httprequest), passing in the ID of the selected country (using GET).
This will return the city dropdown as html with the correct values. You will have the city dropdown in your page in a div, the contents of which you can set through the div's innerHtml attribute when the ajax call has completed, to contain the html returned by the ajax call. If there is no city available it can reture the html for the textbox instead.

I believe that to be the simplest way to do it (and it is dead simple). To find out how ajax works with javascript look it up in a search engine, there are a thousand examples out there. Or this : http://www.webdeveloper.com/forum/showthread.php?t=120372&highlight=ajax