I have tried the example I found on Dynamic select list in the form API (dependent dropdown list)? but I did not get it to work properly. The first list populates, but I can not fill the second one with values that are different depending on the first select.
function formtutorial_form(&$form_state) {
// Connection settings here!!! Then two queries below.
$stmt = $conn->prepare("SELECT productID, ProductName, oil_ID FROM tb_Products");
if ($stmt->execute()) {
$stmt->setFetchMode(PDO::FETCH_OBJ);
$productlist = array();
foreach ($stmt as $row) {
$productlist[$row->productID] = $row->ProductName;
}
}
$productIDselected = $form_state['values']['Products'];
$stmt7 = $conn->prepare("SELECT oil_ID, OilName FROM tb_Oils WHERE oil_ID = $productIDselected ");
if ($stmt7->execute()) {
$stmt7->setFetchMode(PDO::FETCH_OBJ);
$oillists = array();
foreach ($stmt7 as $row) {
$oillists[$row->oil_ID] = $row->OilName;
}
}
$form['Products'] = array(
'#type'=>'select',
'#title' => t('Products'),
'#options' => $productlist,
'#multiple' => FALSE,
'#attributes'=> array('size' => 0),
'#ajax' => array(
'callback' => '_ajaxfunction',
'wrapper' => 'divaroundseconddropdown'
),
);
$form['Oils'] = array(
'#type' => 'select',
'#title' => t('Oils'),
'#options' => $oillists,
'#multiple' => false,
'#prefix' => '<div id="divaroundseconddropdown">',
'#suffix' => '</div>',
'#productlist' => array(
'visible' =>array(
'input[name="productname"]' => array(
'value' => $productlist
),
),
),
);
function _ajaxfunction($form, $form_state) {
return $form['Oils'];
}
function _ajaxfunction($form, $form_state)
). Ifformtutorial_form(&$form_state)
is a form building function, then it is missing an argument, and it should return a value.