In Drupal 7, I have a form where I would like to do the following:
- Display a list of options in a simple two column table
- Selecting an action from a drop down list fires AJAX and replaces options in the table
I have code written and Drupal only posts values to the AJAX on the first time you pick a select. Does this just not work on form 'item' types? Note, there are also other forms on the page, but the form IDs are unique as are the form fields.
$form['new_date'] = array(
'#type' => 'select',
'#title' => 'Delivery Date',
'#options' => $date,
'#ajax' => array(
'callback' => 'delivery_date_weekends',
'wrapper' => 'dates-ajax',
'method' => 'replace',
'effect' => 'fade',
),
);
$form['date_weekend'] = array(
'#type' => 'item',
'#prefix' => '<div id="dates-ajax">',
'#suffix' => '</div>',
'#markup' => '',
);
Then in the AJAX callback, data is returned with #markup set to theme('table') of the results. It only works the first time around, I see the POST data in the Firebug console. Pick another drop down option, my table does not update.
Did I do something wrong here?
Edit: seems like the POST is making it, but the response is empty, but should not be.
Edit 2: Ajax Callback:
function delivery_date_weekends($form, $form_state) {
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'field_collection_item')
->propertyCondition('item_id', $form_state['values']['rates'])
->fieldCondition('field_delivery_basis', 'value', 'Weekly', '=');
$startmonth = strtotime(date('m', $form_state['values']['new_date']).'/01/'.date('Y'));
$endmonth = strtotime('+1 month', $startmonth);
$query->fieldCondition('field_delivery_date_period', 'value', $startmonth, '>=')
->fieldCondition('field_delivery_date_period', 'value', $endmonth, '<=');
$result = $query->execute();
if ($result) {
$headers = array(
array('data' => 'Weekend', 'class' => 'tableHeader-processed'),
array('data' => '', 'class' => 'tableHeader-processed'),
);
$rates = entity_load('field_collection_item', array_keys($result['field_collection_item']));
foreach ($rates as $key => $rate) {
if ($rate->field_delivery_available['und'][0]['value']) {
$rows[] = array('data' => array(date('M j, Y', $rate->field_delivery_date_period['und'][0]['value']), array('data' => 'delivery link', 'class' => array('fee'))));
} else {
$rows[] = array('data' => array(date('M j, Y', $rate->field_delivery_date_period['und'][0]['value']), array('data' => 'Not Available!')));
}
}
$form['booking_weekend']['#markup'] = theme('table', array('header' => $headers, 'rows' => $rows, 'sticky' => FALSE));
} else {
$form['booking_weekend']['#markup'] = 'No delivery weekends exist for this month. Please select another date.';
}
return $form['booking_weekend'];
}
Basically when a month/year is selected, I am querying for dates (inside a field collection).
delivery_date_weekends()
as well?