I have used a select list in my custom form to select a product code and display data corresponding to that code. I am using AJAX for this task but it does not display any record.
function webform_module_menu() {
$items = array();
// Change the description of a form element.
$items['examples/webform_module/report'] = array(
'title' => 'Report',
'page callback' => 'drupal_get_form',
'page arguments' => array('webform_module_report'),
'access callback' => TRUE,
'weight' => 0,
);
return $items;
}
function webform_module_report($form, &$form_state) {
$result = db_query("SELECT o.nid, o.sku FROM {formmodule_order} o ");
$options = array();
foreach($result as $row) {
$options[$row->nid] = $row->sku;
}
$form['select'] = array(
'#title' => 'Option',
'#type' => 'select',
'#options' => $options,
'#ajax' => array(
'wrapper' => 'some-form-results-wrapper',
'callback' => 'webform_module_display'
)
);
$form['results'] = array(
'#prefix' => '<div id="some-form-results-wrapper">',
'#suffix' => '</div>'
);
// If the form has been submitted, build up your results and display them
if (isset($form_state['values']['select'])) {
$rows = db_select('formmodule_order', 't')
->fields('t')
->condition('t.sku', $form_state['values']['select'])
->execute()
->fetchAll();
$header = array(t('Product ID'), t('Product Code'), t('Product Stock'), t('Product Purchased'), t('Created'));
$form['results']['#markup'] = theme('table', array('header' => $header, 'rows' => $rows));
}
else {
$form['results']['#markup'] = '<p>The results will appear here when you change the dropdown option.</p>';
}
return $form;
}
function webform_module_display($form, $form_state) {
return $form['results'];
}