#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    75
    Rep Power
    11

    Drop down list update second list based on first selection


    I have a list of provinces and a list of cities. I want to have the list of cities dynamically displayed depending on the province that is selected without submitting the form.

    Currently the script is working with 2 conditions:

    Condition 1 : User enters the page with an empty $_GET['id']

    This displays all the provinces in the first drop down box and all the cities in the second. If a user selects a province they have to sort through all the cities for Canada which could be error prone.

    Condition 2 : User enters the page with a $_GET['id']

    This works fine. Assuming the user comes to the page through a link the page will preset their province and load the appropriate cities. However; if the user wants to change the province via the drop down box the cities drop down box is not updated.

    My question
    Can I some how make this all easier by coding the following script so that when a user selects a province the city list is updated dynamically? If I can I can probably eliminate my "condition 2".

    My Code:
    PHP Code:


       <p><b>Province:</b><select name="province_id"  size="1">

        <?php //create the pull down menu
         
    echo "<option value=\"\">----------------------</option>\n";
        
    $query "SELECT province_id, province_name FROM province ORDER BY province_name ASC;";
        
    $result = @mysql_query($query);
        while (
    $row mysql_fetch_array($result,MYSQL_ASSOC)) {

            echo 
    "<option value=\"$row[province_id]\"";
            if (isset(
    $_POST['province_id']) && $row[province_id]==$_POST['province_id']) {
                
    $_GET['id'] = $_POST['province_id'];
                echo 
    ' selected="selected"';
            }
            elseif (
    $row[province_id]==$_GET['id']) {
                echo 
    ' selected="selected"'; }

                echo 
    ">$row[province_name]</option>\n";
        }
        
    ?>
        </select></p>


        <p><b>City:</b><select name="city_id" size="1">

        <?php //create the pull down menu

        
    echo "<option value=\"\">----------------------</option>\n";

        if (isset(
    $_GET['id'])) { // if the province is passed in the $_GET the list cities for that province
            
    $query "SELECT city_id, city FROM city WHERE province_id = '".mysql_real_escape_string($_GET['id'])."' ORDER BY city ASC;";
            
    $result = @mysql_query($query);
            while (
    $row mysql_fetch_array($result,MYSQL_ASSOC)) {

            echo 
    "<option value=\"$row[city_id]\"";

            if (isset(
    $_POST['city_id']) && $row[city_id]==$_POST['city_id']) {
                echo 
    ' selected="selected"';
            }


                echo 
    ">$row[city]</option>\n";
        }

            }
    // end of province in $_GET check

        
    else {
        
    $query "SELECT city_id, city FROM city ORDER BY city ASC;";
        
    $result = @mysql_query($query);
        while (
    $row mysql_fetch_array($result,MYSQL_ASSOC)) {

            echo 
    "<option value=\"$row[city_id]\"";

            if (isset(
    $_POST['city_id']) && $row[city_id]==$_POST['city_id']) {
                echo 
    ' selected="selected"';
            }


                echo 
    ">$row[city]</option>\n";
        }
        }
    //end of else

        
    ?>
        </select></p>
    The code above represents the province drop down box first then the cities. Also note that the form is "sticky". Any help is appreciated.

    Cheers,

    CH...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2006
    Location
    Midnight Train Going Anywhere
    Posts
    1,153
    Rep Power
    1593
    This isn't a php issue.

    Javascript anyone?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2008
    Posts
    55
    Rep Power
    6
    Hi Coolhand,

    Because your cities are stored in a DB, your best bet here is to use AJAX. There are good tutorials here w3shools.com & here tizag.com
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    75
    Rep Power
    11
    Ahh.. OK I was hoping there was some magically way to do this with PHP. However, I'll guess I'll start to learn a little JS/AJAX.

    I was trying to avoid it JS where ever possible as it seems more people are turning it off for secuirty reasons.

    Cheers,

    CS...
  8. #5
  9. Expert Debugger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2006
    Location
    Dev Shed Forums (-_^)v
    Posts
    1,022
    Rep Power
    1302
    Originally Posted by CoolHand
    Ahh.. OK I was hoping there was some magically way to do this with PHP. However, I'll guess I'll start to learn a little JS/AJAX.
    there is nothing like magic !

    Originally Posted by CoolHand
    I was trying to avoid it JS where ever possible as it seems more people are turning it off for secuirty reasons.
    around 70% of net user are JavaScript enabled , so its no reason to avoid js, it makes one application more live

    cheers to JavaScript

IMN logo majestic logo threadwatch logo seochat tools logo