I am using the ahah helper as I couldn't seem to get my dropdown lists to depend on each other anyother way in Drupal 6. Below is my code, I can hard code the changes so that the second dropdown populates with the correct values. However when I select a value from dropdown one An error occurred.
/drupal1/?q=ahah_helper/dependent_select
(no information available).
There is no error in my httpd log. All i have changed is, added a DB connection, two queries to populate $grouplist and $sitelist with the latter referencing the selected value from list one for the WHERE clause in the query. I am still passing a ID number which refers to a value. See directly below for the previous code that worked in the example. I have taken the $select1_selected and used that within the query that populates dropdown2.
Dropdown one options = '#options' => array( 1=> 'USA', 2 => 'German'),
Dropdown two options =
if ( $select1_selected == 1 ) {
$select2_options['11'] = 'GM';
$select2_options['12'] = 'Ford';
} else if ( $select1_selected == 2 ) {
$select2_options['21'] = 'VW';
$select2_options['22'] = 'Audi';
}
My Code:
function ahahdrop_menu(){
$items = array();
$items['ahahdrop'] = array(
'title' => 'Ahah Drop menu',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('access ahahdrop'),
'page callback' => 'drupal_get_form',
'page arguments' => array ('ahahdrop_form'),
);
return $items;
}
function ahahdrop_form( $form_state ) {
$form = array();
// this is standard method for register ahah helper for current form
ahah_helper_register($form, $form_state);
// we are building fieldset
$form['dependent_select'] =
array(
'#type' => 'fieldset',
'#title' => t('Dependent Select '),
'#prefix' => '<div id="dependent-select-wrapper">', // specify wrapper element id
'#suffix' => '</div>',
'#tree' => TRUE, // this should be TRUE
);
// provide default option for select one
$select1_selected = 11;
// $form_state['storage'], contains ahah submitted values
if ( isset($form_state['storage']['dependent_select']['select1']) ) {
// get 'select1' selected option
$select1_selected = $form_state['storage']['dependent_select']['select1'] ;
}
try { $conn = new PDO("pgsql:host=localhost port=5432 dbname=riverflies_gis", "opengeo", "opengeo");
echo "Connected";
echo "<br>";
}
catch(PDOException $e) {
echo $e->getMessage();
}
$stmt2 = $conn->prepare("SELECT group_id, rmigroupname FROM rmi_group_tbl ");
if ($stmt2->execute()) {
$stmt2->setFetchMode(PDO::FETCH_OBJ);
$grouplist = array();
foreach ($stmt2 as $row) {
$grouplist[$row->group_id] = $row->rmigroupname;
}
}
$stmt3 = $conn->prepare("SELECT site_id, sitename, group_id FROM sites_tbl WHERE group_id = $select1_selected ");
if ($stmt3->execute()) {
$stmt3->setFetchMode(PDO::FETCH_OBJ);
$siteslist = array();
foreach ($stmt3 as $row) {
$siteslist[$row->site_id] = $row->sitename;
}
}
$form['dependent_select']['select1'] =
array(
'#type' => 'select',
'#title' => t('Select 1'),
'#options' => $grouplist,
'#default_value' => array( $select1_selected ),
// specify ahah event
'#ahah' => array(
'event' => 'change', // this is onchange event of select
'path' => ahah_helper_path(array('dependent_select')),
// provide temporary path
// no need to register it through hook_menu
'wrapper' => 'dependent-select-wrapper',
// provide the wrapper of element
// here we have given the id of fieldset
),
);
$form['dependent_select']['select2'] =
array(
'#type' => 'select',
'#title' => t('Select 2'),
'#options' => $siteslist // set dynamic options
);
$form['submit'] =
array( '#type' => 'submit',
'#value' => t('Save')
);
return $form;
}