Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Populate a dropdown based on another dropdown

    Hello! I'm hoping there is somebody here who can help me on this one. I outsourced a project for my non-profit to an inexpensive source overseas... and he ended up disappearing before the end of the project. I was able to finish most of it myself, but I don't understand this part.

    What I have is a database of free clinics, what services they provide, and what state and county in which they are located. The programmer designed something so that when the page loads, you see one dropdown box (state). When you select a state, another box appears (county, based on the state chosen in the first box). Then a third box appears (services). The third box SHOULD populate based on what's available in that COUNTY, but it just populates with ALL of the possible options.

    Unfortunately, I really don't know how to fix this. I can handle some PHP, but I'm not familiar with JS. I'm a medical student, and this is already the largest database for underserved populations in the US. Can anyone help me out?


    Here's what is in his JS file:

    PHP Code:
    $(function() {

            $(
    '#countydiv').hide();
            $(
    '#servicediv').hide();

            
    // Handler for .ready() called.
            
    $("#select_state").change(function(){
                var 
    stateid = $("#select_state").val();

            $(
    '#select_county').html('');
            $(
    '#countydiv').hide();
            $(
    '#select_service').html('');
            $(
    '#servicediv').hide();

                if(
    stateid != '')
                {
                    $.
    ajax
                    
    ({
                        
    type"POST",
                        
    url"data.php",
                        
    data: {state:stateid},
                        
    success: function(data)
                        {
                            var 
    countyList jQuery.parseJSON(data);

                            
    options '';
                            
    options += '<option value="">Select 'stateid +' - County</option>';
                            for (var 
    1<= countyList.lengthi++) {
                            
    options += '<option value="' countyList[i] + '">' countyList[i] + '</option>';
                            }
                            $(
    "#select_county").html(options);
                            $(
    '#countydiv').show();
                        }
                    });
                }
            });
            $(
    "#select_county").change(function(){
                var 
    countyId = $("#select_county").val();

                 $(
    '#select_service').html('');
                 $(
    '#servicediv').hide();

                 if(
    countyId != '')
                {
                    $.
    ajax
                    
    ({
                        
    type"POST",
                        
    url"data.php",
                        
    data: {county:countyId},
                        
    success: function(data)
                        {
                            var 
    serviceList jQuery.parseJSON(data);
                            var 
    datashow ="";
                            
    options '';
                            
    options += '<option value=""> Select Service </option>';
                            for (var 
    0serviceList.lengthi++) {
                               
    //datashow = serviceList[i].split("#$#");
                            
    options += '<option value="' serviceList[i] + '">' serviceList[i] + '</option>';
                            }
                            $(
    "#select_service").html(options);
                            $(
    '#servicediv').show();
                        }
                    });
                }
            });

        }); 
    Thanks in advance!

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,210
    Thanks
    23
    Thanked 605 Times in 604 Posts
    It looks like the problem may be in data.php or maybe the DB table itself.
    Could you post the file "data.php" please?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you!

    PHP Code:
    <?php


    include("system/config.php");

    $returndata"";

    if(isset(
    $_REQUEST['state']) && !empty($_REQUEST['state']))
    {
        
    $state $_REQUEST['state'];
         
        
    $sql "SELECT County FROM Counties
        WHERE State='"
    .$state."'";
        
        
    $dataset $dbclass_obj->select($sql);
                    
        if(
    count($dataset) > 0)
        {
            for(
    $k=0;$k<count($dataset);$k++)
            {
                
    $returndata[] = $dataset[$k]['County'];
            }
        }
        echo 
    json_encode($returndata);
            die;    
            
    }
    else if(isset(
    $_REQUEST['county']) && !empty($_REQUEST['county']))
    {
        
    $county $_REQUEST['county'];
         
        
    $sql "SELECT Service FROM Services WHERE Service != '' Group by Service";
        
        
    $dataset $dbclass_obj->select($sql);
                    
        if(
    count($dataset) > 0)
        {
            for(
    $k=0;$k<count($dataset);$k++)
            {
                
    $returndata[] = $dataset[$k]['Service'];
            }
        }
        echo 
    json_encode($returndata);
            die;    
            
    }

    /////////////// Total Services ////////////////////
    $totalservice 0;

    $sql "SELECT count( s.ID ) AS cnt_clc
            FROM Services s"
    ;
            
    $dataset $dbclass_obj->select($sql);
                    
    if(
    count($dataset) > 0)
    {
            
    $totalservice $dataset[0]['cnt_clc'];
    }

    //////////////Total Location /////////////////////
    $totallocation 0;

    $sql "SELECT count( c.PID ) AS cnt_clc
            FROM Clinics c"
    ;
            
    $dataset $dbclass_obj->select($sql);
                    
    if(
    count($dataset) > 0)
    {
            
    $totallocation $dataset[0]['cnt_clc'];
    }




    ?>

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,210
    Thanks
    23
    Thanked 605 Times in 604 Posts
    It looks like this is the culprit
    PHP Code:
    $sql "SELECT Service FROM Services WHERE Service != '' Group by Service"
    Don't know the column labels of your DB but guess you have one titled "county". So make a backup of the "data.php" file and then change the above line to:
    PHP Code:
    $sql "SELECT Service FROM Services WHERE county = '".$county."'"
    and see if that works for you.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Tags for this Thread

    Posting Permissions

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