1

Initially there is only drop down menu in the form, when a selection is made I want to display a table in the same form.

$form['blood_group'] = array(
    '#type' => 'select',
    '#title' => t('Blood Group'), 
    '#options' => array(
        'a+' => 'a +ve',
        'b+' => 'b +ve',
        'ab+' => 'ab +ve',
        'o+' => 'o +ve',
        ), 
    '#required' => TRUE,
);

suppose if the user selects a+ i have to show the table containing names of all the people with the same blood group.

1 Answer 1

2

You can take a look at the #ajax property of the Drupal Form API.

Here is a good start :

$form['blood_group_wrapper']['tables'] = array(
  '#prefix' => '<div id="table-wrapper">',
  '#suffix' => '</div>',
);

$form['blood_group_wrapper']['blood_group'] = array(
  '#type' => 'select',
  '#title' => t('Blood Group'), 
  '#options' => array(
      'a+' => 'a +ve',
      'b+' => 'b +ve',
      'ab+' => 'ab +ve',
      'o+' => 'o +ve',
      ), 
  '#required' => TRUE,
  '#ajax' => array(
    'callback' => 'ajax_show_table',
    'wrapper' => 'table-wrapper',
    'method' => 'replace',
    'effect' => 'fade',
  ),
);

function ajax_show_table($form, $form_state) {
  $table = '<table></table>';
  $form['blood_group_wrapper']['tables']['#markup'] = $table;

  return $form['blood_group_wrapper']['tables'];

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.