In this example when a user selects a manufacturer from the drop down the page will automatically narrow down the number available models based on which manufacturer was selected.
First, create a simple action to generate the options portion of a drop down select box.
- Code: Select all
class actions_selectList {
function handle(&$parms) {
// SELECT * FROM <TABLE> WHERE BLAH=BLAH
$app =& Dataface_Application::getInstance();
$auth =& Dataface_AuthenticationTool::getInstance();
$request =& $app->getQuery();
$table =& $request['-table'];
$where =& $request['-where'];
$whereID =& $request['-whereid'];
$val =& $request['-val'];
$text =& $request['-text'];
$records = df_get_records_array($table, array($where=>$whereID));
if ( ! isset($request['-selected']) ) {
echo '<option value="" selected="selected">Please Select ...</option>' . "\n";
}
foreach ( $records as $record ) {
if ( isset($request['-selected']) ) {
echo '<option value="' . $record->val($val) . '" selected="selected">' . $record->val($text) . '</option>';
}
else {
echo '<option value="' . $record->val($val) . '">' . $record->val($text) . '</option>';
}
echo "\n";
}
}
}
Now in the class_<TABLE_NAME> add
- Code: Select all
function block__after_new_record_form() {
echo<<<END
<script language="javascript">
$('#ManufacturerID').change(function() {
if ( $('#ManufacturerID').val() == "" ) {
$('#ModelID').html("");
}
else {
$('#ModelID').html("<option >Loading Models</option>");
$('#ModelID').load('/<My SITE DIR>/index.php?-action=selectList&-table=Models&-val=ModelID&-text=Model&-where=ManufacturerID&-whereid=' + $('#ManufacturerID').val());
}
});
END;
}
</script>