Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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'];    
}
share|improve this question
After lot of hair pulling i edited my code to this level that its flow is all correct in the sense i want to do but it does not displaying any output – aashi Feb 13 at 9:36
Debug Advice: add 'type' => 'item' and '#markup' => '' to your element early state of results element. $form_state has to be by reference in ajax callback (&$form_state) dpm($form_state) inside your callback try to return 'test'; in your callback – Pan Chrono Feb 13 at 10:25
after debugging the query under if statement - $rows = db_select('formmodule_order', 't') ->fields('t') ->condition('t.sku', $form_state['values']['select']) ->execute() ->fetchAll(); does not print any result. It just say array(). what does that mean – aashi Feb 13 at 12:41
print_r($rows)? – Pan Chrono Feb 13 at 16:24
@Pan Chrono yes it prints the array as required but does not display it below the header line – aashi Feb 14 at 5:21

1 Answer

Display recrd from database when selected from select list Using AJAX

yourmodule.module file

<?php

hook_menu implementation

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) {
**query to fetch data in a select list**
$result = db_query("SELECT o.nid, o.sku FROM {formmodule_order} o ");

    $options = array();
   foreach($result as $row) {
      $options[$row->nid] = $row->sku;

    }


 //drupal_set_message('<pre>' . print_r($options, true) . '</pre>');    
 $form['select'] = array(
    '#title' => 'Option',
    '#type' => 'select',
    '#options' => $options,    **//option will be displayed here**
    '#ajax' => array(
      'wrapper' => 'some-form-results-wrapper',
      'callback' => 'webform_module_display'
    )
  );

This will form the header of the table below which the data will be displayed

  $form['results'] = array(
    '#prefix' => '<div id="some-form-results-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#theme' => 'table',
    '#header' => array(t('Product ID'), t('Product Code'), t('Product Stock'), t('Product Purchased'), t('Created')),
    '#rows' => array(),

  );
  // drupal_set_message('<pre>header:' . print_r($form['results'], true) . '</pre>');

  **// If the form has been submitted, build up your results and display them**
  if (isset($form_state['values']['select'])) {

    drupal_set_message('<pre>select_option:' . print_r($form_state['values']['select'], true) . '</pre>');

  $query = db_select('formmodule_order', 't')
      ->fields('t')
      ->condition('nid', $form_state['values']['select']);

     $result = $query->execute();
     // drupal_set_message('<pre>option1:' . print_r($result, true) . '</pre>');
    **// loop through to display result**
   while($row = $result->fetchAssoc()) {


    //$debug .= '<br/>Header Record #'.' <pre>' . print_r($row, true) . '</pre>';
  $nid = array('#id' => 'id',
                 '#type' => 'markup',
                 '#markup' => '<b>' . $row['nid']. '</b>',
                 );
    //drupal_set_message('<pre>' . print_r($nid, true) . '</pre>');
    $sku = array('#id' => 'sku',
                     '#type' => 'markup',
                    '#markup' => '<b>' . $row['sku'] . '</b>',
                 );
    //drupal_set_message('<pre>' . print_r($sku, true) . '</pre>');
   $orderin = array('#id' => 'orderin',
                 '#type' => 'markup',
                 '#markup' => '<b>' . $row['orderin']. '</b>',
                 );
            //   print_r($orderin);
    //drupal_set_message('<pre>' . print_r($nid, true) . '</pre>');
    $orderout = array('#id' => 'orderout',
                     '#type' => 'markup',
                    '#markup' => '<b>' . $row['orderout'] . '</b>',
                 );
    //drupal_set_message('<pre>' . print_r($orderOut, true) . '</pre>');
    $created = array('#id' => 'created',
                     '#type' => 'markup',
                    '#markup' => '<b>' . $row['created'] . '</b>',
                 );

    $form['results'][] = array('nid' => &$nid,
                                'sku' => &$sku,
                                'orderIn' => &$orderin,
                                'orderOut' => &$orderout,
                                'created' => &$created,
                                );
    $form['results']['#rows'][] = array(array('data' => &$nid),
                                         array('data' => &$sku),
                                         array('data' => &$orderin),
                                        array('data' => &$orderout),
                                         array('data' => &$created),
                                         );
    unset($nid);
    unset($sku);
    unset($orderin);
    unset($orderout);
     unset($created);


    }              


  }


 else {
   $form['results']['#rows'] = '<p>The results will appear here when you change the dropdown option.</p>';
    }

      return $form;
    }

**callback function** 
   function webform_module_display($form, $form_state) {

      return $form['results'];


    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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