Hi rodiusd,
I have found a recent example of a triple-drop down that may help . The code looks long and complex but isn't too bad once you break it into chunks.
Here is the code from the Ajax event:
<?php
defined('_JEXEC') or die('Restricted access');
//echo'<div>$form->data: '.print_r($form->data, true).'</div>';
$db =& JFactory::getDBO();
switch ( $form->data['r'] ) {
default:
return false;
break;
case 'hose':
$query = "
SELECT h.`hose_id` AS value,
CONCAT(h.`hose_id`, ' - ', t.`hose_type_name`, ' (', IF( h.`has_5_16` = '1', '5/16', '1/4'), ')') as text,
h.part_no AS data_part_no,
p.`img_name` AS data_image
FROM `#__hc_hose` AS h
INNER JOIN `#__hc_hose_type` AS t ON t.`hose_type` = h.`type`
INNER JOIN `#__hc_part` AS p ON h.`part_no` = p.`part_no`
WHERE `gas_id` = '{$form->data['gas']}';
";
$db->setQuery($query);
$data = $db->loadAssocList();
echo json_encode($data);
break;
case 'inlet':
$swivel = "`swivel` = ''";
if ( $form->data['swivel'] == 1 ) {
$swivel = "`swivel` != ''";
}
$query = "
SELECT f.`inlet_code` AS value,
CONCAT(f.`inlet_code`, ' - ', p.`part_desc`) AS text,
f.`part_no` AS data_part_no,
p.`img_name` AS data_image
FROM `#__hc_fitting` AS f
INNER JOIN `#__hc_part` AS p ON f.`part_no` = p.`part_no`
WHERE `hose_id` = '{$form->data['hose']}'
AND f.`inlet_code` != ''
AND {$swivel} ;
";
$db->setQuery($query);
$data = $db->loadAssocList();
echo json_encode($data);
break;
}
$app =& JFactory::getApplication();
$app->close();
?>
Note here that I am using $form->data['r'] to identify which query to run and $form->data['hose'] or $form->data['gas'] to pass the data for that query.
The queries are compound - they are getting data from more than one table; and I am passing back some data items as well as the value and text.
Here is part of the code from the Load JS action which handled one of these calls:
hose_req = new Request.JSON({
url: 'index.php?option=com_chronoforms&chronoform=hose_configurator_a&event=ajax_a&r=hose',
method: 'get',
onRequest: function() {
hose.empty();
new Element('option', {
'value': '',
'text': 'Loading . . .'
}).inject(hose);
},
onSuccess: function(resp) {
//console.log(resp);
hose.empty();
new Element('option', {
'value': '',
'text': '(please select)'
}).inject(hose);
resp.each(function(r) {
new Element('option', {
'value': r['value'],
'text': r['text'],
'data-part': r['data_part_no'],
'data-image': r['data_image']
}).inject(hose);
});
hose.disabled = false;
hose.fireEvent('change');
},
onFailure: function() {
hose.empty();
new Element('option', {
'value': '',
'text': 'Loading failed.'
}).inject(hose);
}
}).post({'gas': gas.value});
});
This uses new request.JSON() to make the call with some methods; the main one is onSuccess() which empties the 'hose' select list then builds and adds a new set of options for it.
Bob