0

I have a website where info of registered member from various countries and states are collected. On my search form, I have 3 fields; Country, State and Sex.

I listed all the countries of the world in my search (as a dropdown), but the state field is empty. Want I want is that once a visitor select a country, i want only the states of that country which registered members have are in my database to be pulled into the state field, instead of all the state of that country.

Eg 3 members from USA are from New York, New Jersey and Georgia. On selecting USA in the country dropdown, only these 3 state should appear under the state instead of the 50 states in america.

rayfranco
  • 3,630
  • 3
  • 26
  • 38
adsegzy
  • 11
  • 3
  • You should consider adding more details, like the mysql queries that you are generating. And maybe the javascript part if you think it could be pertinent. – rayfranco Nov 20 '12 at 15:52

2 Answers2

0

You could pull the the states of the registered members from your database using php or any server-side language you are using. Then you can use AJAX to get the states as an xml or a JSON object. You can the use the members of the object to populate the dropdown.

Example sql query string to get states of registered users: Select state From user_details WHERE country = 'USA'. This should return the states (probably in an array);

Example PHP code that will be retrieved by an AJAX call: $reply = {states: [NJ, NY, AZ]}. Example JS code to parse the above reply: var reply = JSON.parse(serverReply); var states = reply.states; . I hope this helps

NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
0

You would need to do a script, for example, in php(or any server side language), that does the query, it would look like:

$countryId = $_POST['countryId'];

$sql = "SELECT fields FROM states WHERE countryId = $countryId";
$result = .... etc;

returning an json object for example to the main page.

And from the main page you should do a Ajax request to the php page, getting the json object returned depending on your option selected and populating the next field.

You could have a look at jQuery framework as at least I, find easier than using raw javascript.

aleation
  • 4,796
  • 1
  • 21
  • 35