0

I am using Drupal 7, I am using form API to generate a table with select option filters. The data from the table is from an external API.

The table loads as it should, the drop down selection loads as should. When I select an option from the drop down section I am trying to pass the selected value into the URL of the external API and rebuild the table using the new URL but the table does not get rebuilt, instead the page just refreshes, the selected option is updated in the drop down selection. I have tested and think that the URL variable is updated to include the select option but the table does not get rebuilt.

This is the module

function mymodule_form($form, &$form_state) {

$form['type_options'] = array(
  '#type' => 'value',
  '#value' => $options
);

$form['Title'] = array(
  '#title' => $title,
  '#type' => 'select',
  '#options' => $form['type_options']['#value'],
  '#default_value' => isset($form_state['storage']['Title']) ? $form_state['storage']['Title'] : '',
);

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t(“Filter”),  
);

//Get fromtable data from API
if (!empty($form_state['storage']['Title'])) {
 $filter = preg_replace('/\s+/', '+', $form_state['storage']['Title']);
  $url  = $api.'&filter='.$filter;
} else {
  $url = $api;
}
$json = file_get_contents($url);
$data = json_decode($json,true);
$rows = $data[data];

//Set paging items for formtable
$per_page = 30;
$current_page = pager_default_initialize(count($rows),$per_page);
$chunks = array_chunk($rows, $per_page, TRUE);
$form['table'] = array(
  '#theme' => 'table',
  '#header' => $header,
  '#rows' => $chunks[$current_page],
  '#empty' => $empty,
  '#pager' => array('quantity' => 12)
);      

$form['pager'] = array(
  '#theme' => 'pager',
);

return $form;
}

function mymodule_form_submit($form, &$form_state) {
  $form_state['storage']['Title'] = $form_state['values']['Title'];
  $form_state['rebuild'] = TRUE;  
}

Thanks for any help

0

Your code should work fine from what I see. You can print the $url value to see what really is passed to API. Please note that $form_state['storage']['Title'] will contain the key, not value - maybe that's the problem?

Plus, you don't need to use storage in this case. When using $form_state['rebuild'] you can just use $form_state to pass variables, like this:

function mymodule_form($form, &$form_state) {
  $storage_val = isset($form_state['values']['title']) ? $form_state['values']['title'] : 'nothing stored yet';

  $form['message'] = array(
    '#markup' => t('Stored value: %storage_val', array('%storage_val' => $storage_val)),
  );

  $options = array(
    'opt1' => 'option1',
    'opt2' => 'option2',
    'opt3' => 'another option',
  );

  $form['title'] = array(
    '#title' => 'Select option',
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => isset($form_state['values']['title']) ? $form_state['values']['title'] : '',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Filter'),
  );

  return $form;
}

function mymodule_form_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

Your Answer

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

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