Link to home
Start Free TrialLog in
Avatar of Skotch5
Skotch5

asked on

Setting two Dynamic Selects from PHP and Javascript

Hi all, trying to create two select dropdowns in a profile.

Mission is to update the second select dropdown with a selection of models relating to the selected make for the first select dropdown (without page refresh if possible) and then update the members profile in the database with these values.

Currently I have two functions (see below) that are working fine to update both seperate fields, but I am at a lost trying to combine them into one, so any help would be great.

The "make" table contains "id", and "make" fields; with the "model" table containg "id", "make_id", "model", where the model is matched to the make on make_id, if that helps.

An example of how it should work can be seen on this site: http://www.nissanusa.com/vehicles/Configurator/1,9442,,00.html

Thankyou for any help you can provide :)

Two functions I am currently using called from a PHP file as:
      'MAKE' => vehicle_make_select($garage_userdata['make']),
      'MODEL' => vehicle_model_select($garage_userdata['model']),
and shown by a template.

function vehicle_make_select($default, $select_name = 'make')
{  
        global $db, $lang;
               
        $sql = "SELECT id, make
              FROM " . GARAGE_MAKES_TABLE . "
                ORDER BY make";    
      if ( !($result = $db->sql_query($sql)) )
      {
            message_die(GENERAL_ERROR, 'Could not obtain list of Car Makes.', '', __LINE__, __FILE__, $sql);
    }
       
        $vehicle_make_select ='<select name="' . $select_name . '">';
    $vehicle_make_select .= '<option value="">' . $lang['C_Select_make'] . '</option>';
       while ( $row = $db->sql_fetchrow($result) )
        {  
             $selected = ( $row['id'] == $default ) ? ' selected="selected"' : '';
            $vehicle_make_select .= '<option value="' . $row['id'] . '"' . $selected . '>' . $row['make'] . '</option>';
      }
      $vehicle_make_select .= '</select>';
        return $vehicle_make_select;
}


function vehicle_model_select($default, $select_name = 'model')
{  
        global $db, $lang;
 
        $sql = "SELECT *
              FROM " . GARAGE_MODELS_TABLE . "
               ORDER BY model";
         
      if ( !($result = $db->sql_query($sql)) )
      {
            message_die(GENERAL_ERROR, 'Could not obtain list of Car Models.', '', __LINE__, __FILE__, $sql);
    }

        $vehicle_model_select ='<select name="' . $select_name . '">';
    $vehicle_model_select .= '<option value="">' . $lang['C_Select_model'] . '</option>';
       while ( $row = $db->sql_fetchrow($result) )
        {
          $selected = ( $row['id'] == $default ) ? ' selected="selected"' : '';
          $vehicle_model_select .= '<option value="' . $row['id'] . '"' . $selected . '>' . $row['model'] . '</option>';
    }
        $vehicle_model_select .= '</select>';
        return $vehicle_model_select;
}


ASKER CERTIFIED SOLUTION
Avatar of Georgiana Gligor
Georgiana Gligor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial