Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.


Results 1 to 4 of 4

Thread: Form with country and state fields from MySQL database

  1. #1

    Pallete Form with country and state fields from MySQL database

    Helo everybody!

    I'm trying to find a code for implementing in a php form. The form must contain two selects: one for a country and one for the state. All the data is taken from a database.

    I suppose ajax and javascript are needed... because I want that the second select autopopulate depending on the value selected in the first select: country.

    I can't believe there is no code with this solution.

    any ideas?

    Thank you!

  2. #2
    Of course there's a code, you just have to do it yourself!
    AJAX would be good.
    Grab the value of the select with an onchange event, send it to the server, query the database, send back the values for the second select box.
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  3. #3

    Fla Box

    Thank you, I will try doing it by myself. Best regards.

  4. #4
    If there's not too much data, you can load it all on one page, but hide from user with help of CSS:

    <select name="USA_states" style="visibility:hidden">
    <option value="AZ">Arizona
    <option value="TX">Texas

    You can put such hidden SELECT-s for all countries on a single page.
    Then make use of JavaScript to show the right SELECT for a state, when user selects a country:

    <select name=country onchange=countryChanged(this.value)>
    <option value=USA>United States
    <option value=Canada>Canada

    Depending on the value, you can show/hide the right state SELECT:

    forms[0].elements[country+"_select"].style.visibility = ''; // shows
    forms[0].elements[country+"_select"].style.visibility = 'hidden'; // hides

    Good luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012