Please give me some solution for this. I want to display the Database select query results in a form using ajax in drupal 7.
1 Answer
Without specifics it's tricky to answer your question but here's a very basic example:
function MYMODULE_some_form($form, &$form_state) {
$form['select'] = array(
'#title' => 'Option',
'#type' => 'select',
'#options' => drupal_map_assoc(array(1,2,3)),
'#ajax' => array(
'wrapper' => 'some-form-results-wrapper',
'callback' => 'MYMODULE_some_form_ajax'
)
);
$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('some_table', 't')
->fields('t')
->condition('some_field', $form_state['values']['select'])
->execute()
->fetchAll();
$header = array();
$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 MYMODULE_some_form_ajax($form, &$form_state) {
return $form['results'];
}
You might want to check out the ajax_example
module included as part of the Examples module, it has a lot of code and comments that will help you understand what every piece of the above example does.