0

I have two elements in my form: a select element and a div with an id of #myid_table_wrapper.

function myid_print_user_page_form()
{
    $form = array();

    $form['options'] = array(
        '#type' => 'select',        
        '#options' => array(
            0 => t('All'),
            1 => t("Printed ID's"),
            2 => t("Unprinted ID's")
        ),       
        '#ajax' => array(
            'callback' => 'myid_print_all_submitted_id_callback',
            'wrapper' => 'myid_table_wrapper',
        ),                             
    );
    $form['myid_table_wrapper'] = array(
        '#type' => 'item',                
        '#markup' => '<div id= "myid_table_wrapper"></div>',                              
    );
    return $form;
}

Everytime the select element fires an onchange event it calls a callback function named myid_print_all_submitted_id_callback.

function myid_print_all_submitted_id_callback($form, $form_state)
{    
    // Build the sortable table header.
    $header = array(
        'id_number' => array('data' => t('ID Number'), 'field' => 'id_number','sort' => 'asc'),     
        'student' => array('data' => t('Student'), 'field' => 'student', 'sort' => 'asc')
   );

   //Get the node data. 
   $query = db_select('student', 't1',array('target' => 'import'));
   $result = $query
       ->fields('t1', array('studid','lastname', 'firstname', 'middlename', 'extname'))   
       ->condition('t1.id_status',2,'=')
       ->distinct()
       ->execute();
   //Build the rows.
   $rows = array();  
   foreach($result as $record){
       $rows[$record->studid] = array(
           'id_number' => $record->studid,
           'student' => $record->firstname . ' ' . $record->middlename . ' ' . $record->lastname . ' ' . $record->extname,    
       );
   }
   return theme('table', array('header' => $header, 'rows'=> $rows));
}

The code results to an error:

enter image description here

Whats wrong with my code? How will I debug this?

1

The correct way to do this is to populate the id table inside the form generation function (checking $form_state['values']). Then the callback should only return the form element.
This is an example of how I would do it:

function myid_print_user_page_form($form, $form_state)
{
    $form = array();

    $form['options'] = array(
        '#type' => 'select',        
        '#options' => array(
            0 => t('All'),
            1 => t("Printed ID's"),
            2 => t("Unprinted ID's")
        ),       
        '#ajax' => array(
            'callback' => 'myid_print_all_submitted_id_callback',
            'wrapper' => 'myid_table_wrapper_div',
        ),                             
    );
    $table_html = '';

    // Check if option has been selected.
    if(!empty($form_state['values']['options'])){
      // Build the sortable table header.
      $header = array(
          'id_number' => array('data' => t('ID Number'), 'field' =>   'id_number','sort' => 'asc'),     
          'student' => array('data' => t('Student'), 'field' => 'student', 'sort' => 'asc')
       );
       //Get the node data. 
       $query = db_select('student', 't1',array('target' => 'import'));
       $result = $query
           ->fields('t1', array('studid','lastname', 'firstname', 'middlename', 'extname'))   
           ->condition('t1.id_status',2,'=')
           ->distinct()
           ->execute();
       //Build the rows.
       $rows = array();  
       foreach($result as $record){
           $rows[$record->studid] = array(
               'id_number' => $record->studid,
               'student' => $record->firstname . ' ' . $record->middlename . ' ' . $record->lastname . ' ' . $record->extname,    
           );
       }
       $table_html = theme('table', array('header' => $header, 'rows'=> $rows));


    }

    $form['myid_table_wrapper'] = array(
        '#type' => 'item',                
        '#markup' => $table_html,
        '#prefix' => '<div id="myid_table_wrapper_div">',
        '#suffix' => '</div>',                              
    );
    return $form;
}

function myid_print_all_submitted_id_callback($form, $form_state)
{
    return $form['myid_table_wrapper'];
}

This is untested, but you get the idea.

7
  • Thanks for giving me an idea, I tried what you said, but my table only rebuilds when the last option is selected. When the first two options are selected, the same error occurs. Where am I missing? – user46175 Apr 15 '15 at 7:04
  • Something's wrong with the line below: – user46175 Apr 15 '15 at 7:09
  • 'student' => $record->firstname . ' ' . $record->middlename . ' ' . $record->lastname . ' ' . $record->extname, – user46175 Apr 15 '15 at 7:09
  • I edited the code to: 'student' => 'Constant String'. Then, it perfectly runs. Why? – user46175 Apr 15 '15 at 7:11
  • 1
    also, might be a good idea to send it through the check_plain function 'student' => check_plain($record->firstname . ' ' . $record->middlename . ' ' . $record->lastname . ' ' . $record->extname) – 2pha Apr 15 '15 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy