1

I want to show my result from the form in the table on the same page.

//my form
function find_tryk_filter_test_form_page($form, &$form_state)
{
$form['submit']         = array(
    '#type' => 'submit',
    '#value' => t('Find trykluft element'),
    '#ajax' => array(
        'callback' => 'find_tryk_filter_test_ajax_result_callback',
        'wrapper' => 'result-wrapper',
        'method' => 'click'
    )
);

return $form;
}

My page callback function

function find_tryk_filter_test_page()
{
  $page = drupal_render(drupal_get_form('find_tryk_filter_test_form_page'));
  $page .= filter_elementer_rows($form_state['values']['dropdown_third']);
  print_r($page);
  return $page;
}

The function that retrives my data and create the table

function filter_elementer_rows($value_dropdown_third)
{
  $header = array(
    'Navn',
    'Shoplink',
    'Brochure'
   );
  $query  = db_query(//Some sql statement...));
  $rows   = array();
  foreach ($query as $row) {
    $rows[] = array(
        $row->title,
        $row->field_shoplink_value,
        $row->uri
    );
}
  return theme('table', array(
    'header' => $header,
    'rows' => $rows
  ));
}

My submit function

function find_tryk_filter_test_page_submit($form, &$form_state)
{
   return find_tryk_filter_test_page();
}

My problem is that when I run this, I get absolutely nothing in the table. And the SQL does work. Do I miss anything?

1 Answer 1

1

The return value from a form submit function isn't used. What you need to to is rebuild the form, and alter the original form function to display the table if it's available. For example:

function find_tryk_filter_test_form_page($form, &$form_state) {
  if (isset($form_state['render_table'])) {
    $form['table'] = array('#markup' => find_tryk_filter_test_page());
  }

  $form['submit'] = array( 
   '#type' => 'submit',
    '#value' => t('Find trykluft element'),
    '#ajax' => array(
        'callback' => 'find_tryk_filter_test_ajax_result_callback',
        'wrapper' => 'result-wrapper',
        'method' => 'click'
    )
  );

  return $form;
}

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

Or you might redirect to a page that has your table on it, if that makes sense:

function find_tryk_filter_test_page_submit($form, &$form_state) {
   $form_state['redirect'] = 'foo/bar/path';
}
2
  • I still don't get any result. Aug 15, 2014 at 8:56
  • is that necessary to define the table in the form as a form element? something like this: $form['render_table'] = array( '#theme' => 'table', '#rows' => some array, '#header' =>some array ); Aug 15, 2014 at 8:58

Your Answer

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

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