SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Belgian in Mexico
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post refresh listbox values (countries, cities,...)

    Hi,

    In my signup form, I ask for the user's country, state and city. I've seen a few javascript snippets to filter the state according to the country and then the city, according to the state, during the signup process, but they were using arrays and were manually populated.

    How could I do that, using a mysql database with the countries, states and cities? I already have a DB with that info (as well as languages (by % of population), head of state, GNP, population, etc...) for more than 200 countries (and about 4,100 cities), so I prefer using it than having to manually populate very large arrays.

    I suck at Javascript, so I'd really appreciate if someone could post some code, besides giving guidelines to do it.

    If anyone is interested in the DB, I could post the link to download the .sql.

    Thanks a lot.

    MichaŽl Niessen

  2. #2
    SitePoint Addict streetlife's Avatar
    Join Date
    Mar 2004
    Location
    D-Town
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can either have all data listed in arrays in javascript, or you can do a background server query upon a selection change.
    As for putting the data into a js array, if you have no problem pulling data out of the db and working with it then it's pretty simple really, it's just a matter of looking at how to define an array in js, then creating a variable with a string which contains the js array, and then printing that to the page:
    PHP Code:
    $jsArray='var myarray=["'.$valueA.'","'.$valueB.'","'.$valueC.'"];';
    echo 
    "<script>".$jsArray."</script>"
    Hopefully you get the gist with that, I recommend creating a function that takes a regular php array and returns a string with that same array as a js array declaration.
    Once you've got that going on it's all about referencing page objects in javascript which is really a topic for the javascript forum.

  3. #3
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Belgian in Mexico
    Posts
    307
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the tip. Anyways, while playing around, I found another way to do it. And it was easier for me (as I'm really not good at Javascript)than your solution.

    I used:
    onchange="document.forms['form1'].submit()
    in the select tags for the countries, states and cities lists. Also, Before generating the state list, the code check if the country id has been posted. If not, an empty list is displayed.

    MichaŽl Niessen

  4. #4
    SitePoint Addict streetlife's Avatar
    Join Date
    Mar 2004
    Location
    D-Town
    Posts
    369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okcool, I though you were after a more dynamic way of doing it is all.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mniessen
    Thanks for the tip. Anyways, while playing around, I found another way to do it. And it was easier for me (as I'm really not good at Javascript)than your solution.

    I used:
    onchange="document.forms['form1'].submit()
    in the select tags for the countries, states and cities lists. Also, Before generating the state list, the code check if the country id has been posted. If not, an empty list is displayed.

    MichaŽl Niessen

    hey if your still here, just want to ask you said "the code check if the country id has been posted if not an empty list is displayed, are you referring to PHP code ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •