I'm really stuck with a form that I am building. Basically I have 3 select boxes,
- Project
- Consultant
- Work Agreement
The selection of Project populates Consultant and the selection of Consultant populates Work Agreement. The selection of Work Agreement then brings up the details for the selected Work Agreement node in a div below it.
Like so:
$form = array();
drupal_add_js(drupal_get_path('module', 'crs_timesheets') . '/crs_timesheets.js');
ahah_helper_register($form, $form_state);
// Set default values as per helper instructions
if (!isset($form_state['post']['timesheet_info']['project'])) {
$project_id = 0;
$consultant_id = 0;
$work_agreement_id = 0;
} else {
$project_id = $form_state['post']['timesheet_info']['project'];
}
if (!isset($form_state['post']['timesheet_info']['consultant'])) {
$consultant_id = 0;
$work_agreement_id = 0;
} else {
$consultant_id = $form_state['post']['timesheet_info']['consultant'];
}
if (!isset($form_state['post']['timesheet_info']['work_agreement'])) {
$work_agreement_id = 0;
} else {
$work_agreement_id = $form_state['post']['timesheet_info']['work_agreement'];
}
if ($work_agreement_id != 0) {
$wa_display = display_work_agreement($work_agreement_id);
} else {
$wa_display = '<div></div>';
}
$form['timesheet_info'] = array(
'#type' => 'fieldset',
'#prefix' => '<div id="timesheet-info-wrapper">', // This is our wrapper div.
'#suffix' => '</div>',
'#tree' => TRUE, // Don't forget to set #tree!
);
$contracts[0] = 'Please Select';
$array = get_associated_contracts($company->nid, 'select');
foreach ($array as $key => $value) {
$contracts[$key] = $value;
}
// Project select box, populated by simple sql script (not shown)
$form['timesheet_info']['project'] = array(
'#title' => 'Contract/Purchase Order/Project',
'#description' => 'Select which contract, purchase order or project that the timesheet relates to',
'#type' => 'select',
'#options' => $contracts,
'#ahah' => array(
'event' => 'change',
'progress' => array('type' => 'throbber'),
'path' => ahah_helper_path(array('timesheet_info')),
'wrapper' => 'timesheet-info-wrapper',
),
'#default_value' => $project_id,
);
// Consultants select box. Populated by consultants assigned to above selection, using sql (not shown)
$form['timesheet_info']['consultant'] = array(
'#title' => t('Consultant'),
'#description' => 'Please select the consultant you wish to add a timesheet for',
'#type' => 'select',
'#options' => get_associated_contractors($project_id),
'#required' => TRUE,
'#ahah' => array(
'event' => 'change',
'progress' => array('type' => 'throbber'),
'path' => ahah_helper_path(array('timesheet_info')),
'wrapper' => 'timesheet-info-wrapper',
),
'#default_value' => $consultant_id,
);
// Work agreement, populated by sql using selection of above select boxes
$form['timesheet_info']['work_agreement'] = array(
'#title' => 'Work Agreement',
'#type' => 'select',
'#ahah' => array(
'event' => 'change',
'progress' => array('type' => 'throbber'),
'path' => ahah_helper_path(array('timesheet_info')),
'wrapper' => 'timesheet-info-wrapper',
),
'#options' => get_work_agreements($project_id, $consultant_id),
'#default_value' => $work_agreement_id,
);
// Work agreement details. This is where my problem is. This continues to display the previous work agreement if project or //consultant are changed, even if that then means there are no work agreements related to that at all. I need to it to be
//replaced by empty divs EVERYTIME either consultant or project is changed.
$form['timesheet_info']['work_agreement_info'] = array(
'#type' => 'markup',
'#prefix' => '<div id="work_agreement_wrapper">',
'#suffix' => '</div>',
'#value' => $wa_display,
);
$form['timesheet_info']['create_timesheet'] = array(
'#type' => 'submit',
'#value' => 'Create TimeSheet'
);This all works great, except that when I then change the selection of either consultant or project, the work agreement div still displays the old details, because it;s obviously still submitting that value to the function that populates it. I tried using JQuery to reset the values to 'Please Select', but it seems the submit happens before that, as it changes it, then repopulates it when the AHAH finishes running. I've used [dsm][1]() to output form_state and it is indeed submitting values that are then removed from the options, and basically shouldn't be available.
(function($) {
$(document).ajaxComplete(function(e, xhr, settings)
{
$('#edit-timesheet-info-project').change(function() {
$('#work_agreement_wrapper').replaceWith('<div id="work_agreement_wrapper">testtesteestetsa</div>');
$('#edit-timesheet-info-consultant').val(0);
$('#edit-timesheet-info-work-agreement').val(0);
});
});
})(jQuery);I've also tried running it using document.ready to check, but ahah overwrites it when the form rebuilds.
I would try and use $form to check if the values submitted are the same as the original values, then I can change the default value in PHP, but form isn't available to me until submission.
If anyone can help, perhaps with a delay on the ahah submission maybe? Been stuck on this for a while and it looks really poor.
Comments
Comment #1
mykmallett commentedI'm such an idiot. As soon as I typed this out I realised I could just put the if statement in the ajaxcomplete function.
Thanks!
Comment #2
mykmallett commentedComment #2.0
mykmallett commentedAdded meaningful comments