A simple example:


  function test_form() {

        $form['serial'] = array(
                '#type' => 'textfield',
                '#title' => t('serial number'),
        );

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

        $form['#redirect'] = FALSE;

        $output = drupal_get_form('test_form', $form);
        return $output;
  }

  $output = test_form();
  echo "$output";

will display the submit button below the textfield.
Now I want to position the submit button on the same line, after the textfield, using a table.

How do I accomplish that?

Comments

Budrick’s picture

Lasy to search for examples, just can point to check '#prefix' and '#suffix' options.

Or, better to produce table with theme('table', )

blond_and_stupid’s picture

I tried using #prefix and #suffix without success.
Where can one find easy-to-understand examples?

Budrick’s picture

Theming forms
theme_table (use: theme('table', $header, $rows, $attributes, caption);)

blond_and_stupid’s picture

thanks for your help, but I still can't figure out how to do this.
What I want is roughly the following simple php code...

echo "<table border='0'><tr>";
echo "<td><input type='text' size='5' name='serial'></td>";
echo "<td><input type='submit' value='submit'></td>";
echo "</tr></table>";

to get the textfield and submit button on the same line.

Budrick’s picture

blond_and_stupid’s picture

OK, I got it to work using #prefix and #suffix.
For those interested, I'll post my original test form modified to display the textfield and submit button on the same line instead of on separate lines.
It was a simple solution, yet it took me some hours to figure out.
Thanks Budrick for your help, especially the last link "Writing forms in tables" was exactly what I needed.

  function test_form() {

        $form['serial'] = array(
                '#type' => 'textfield',
                '#title' => t('serial number'),
                '#prefix' => '<table><tr><td>',
                '#suffix' => '</td>',
        );

        $form['submit'] = array (
                '#type' => 'submit',
                '#value' => t('Submit'),
                '#prefix' => '<td>',
                '#suffix' => '</td></tr></table>',
        );

        $form['#redirect'] = FALSE;

        $output = drupal_get_form('test_form', $form);
        return $output;
  }

  $output = test_form();
  echo "$output";
vaishnavig’s picture

Hi,
Thank you for posting this code.
I was searching for some way to display my form elements in a table.
Thanks again.

mikou’s picture

Hello,

That seems to be exactly what I am looking for. But I can't figure out where to write all this :(

Any link ? Any help ?
Thanks in advance ;)

vigneshr35’s picture

Thank u guys..i was searching for this piece of code for long time..hope it will be useful for many users like me... :)

jm.federico’s picture

Hi Guys

Found the proper solution. It might look scary at first, but once you get it is all good.

To begin with you should read http://drupal.org/node/751826, pay spacial attention to "Theming Forms".

Once you've read that you can take a look at http://api.drupal.org/api/function/theme_node_admin_nodes/6
That is part of the implementation for the well known 'admin/content/node/overview' page in every Drupal install.

Long story short (read above links anyway) you render each form element that is to go on a table independently and at the end you render whatever is left of the form that you do not want on a table, Drupal is smart enough not to render something twice (just brilliant).

Example:

You need to create your own theme_WHATEVER function that will render you form, and make your form use your theme function:

function your_form($form_state) {
...
  $form['#theme'] = 'your_theme_hook';
...
  return $form
}
function your_theme_hook($form) {
  // Here you do whatever you want
  // e.g. Lets put some parts of the form on a table (that's what we want, right?)
  $header = array(t('Name'), t('User'), t('Status'));
  foreach (element_children($form['name']) as $key) {
    $row = array();
    $row[] = drupal_render($form['name'][$key]);
    $row[] = drupal_render($form['username'][$key]);
    $row[] = drupal_render($form['status'][$key]);
    $rows[] = $row;
  }
  // This is where we create the table using theme()
  $output .= theme('table', $header, $rows);
  // Now we render whatever is left of the table, for instance the submit buttons
  $output .= drupal_render($form);
  // Magic, return your form
  return $output;
}

Last, register your theme to Drupal using hook_theme()

/**
 * Implements hook_theme().
 */
function your_theme_theme($existing, $type, $theme, $path) {
  return array(
    'your_theme_your_theme_hook' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}
esnyder’s picture

Jay: Great tutorial! Here is the link to the D7 tutorial of the same subject. It is much easier than D6!
http://www.jaypan.com/blog/themeing-drupal-7-forms-tables-checkboxes-or-...

texas-bronius’s picture

Avoid spreading your code across a bunch of locations with something like the following FAPI snippet:

  $form['section_group_mapper']['section'] = array(
    '#type' => 'markup',
    '#prefix' => '<table>',
    '#suffix' => '</table>',
  );
  foreach ($csv_sections as $section => $count) {
    $form['section_group_mapper']['section'][$section.'_label'] = array(
      '#type' => 'markup',
      '#prefix' => '<tr><td>',
      '#suffix' => '</td>',
      '#markup' => "$section ($count participants)",
    );
    $form['section_group_mapper']['section'][$section] = array(
      '#type' => 'select',
      '#prefix' => '<td>&rarr;</td><td>',
      '#suffix' => '</td></tr>',
      '#options' => array('hoo hoo'),
    );
  }

to get:
sample fapi table row output
You'd want to put in id and class attributes appropriate to your application, but the stub functionality is here.

--
http://drupaltees.com
80s themed Drupal T-Shirts

AlxVallejo’s picture

That's great if you want a tableselect, but that doesn't really help if you want inputs in the table cells.

Jaypan’s picture

You can use this tutorial for that: http://www.jaypan.com/tutorial/themeing-drupal-7-forms-including-css-and-js

The difference will be that in the theme function (called theme_form_theme_form()), you will build and return your table.

bbu23’s picture

You might want to change $output .= drupal_render($form); into $output .= drupal_render_children($form);.

datarazor’s picture

This might help you to avoid writing code:

https://drupal.org/project/field_group_table

Nithyanantham’s picture

How to create and get textfield value from custom table using custom module