How to use tables in Form API

blond_and_stupid - August 23, 2006 - 16:54

A simple example:

<?php
 
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?

2 ways

Budrick - August 23, 2006 - 18:08

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

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

Examples?!

blond_and_stupid - August 24, 2006 - 07:48

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

-

Budrick - August 24, 2006 - 10:15

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

still puzzled...

blond_and_stupid - August 25, 2006 - 12:00

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...

<?php
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.

Writing forms in tables

Budrick - August 26, 2006 - 04:45

Finally!

blond_and_stupid - August 26, 2006 - 06:59

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.

<?php
 
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";
?>

Thanks for posting this code

vaishnavig - July 18, 2008 - 10:21

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

 
 

Drupal is a registered trademark of Dries Buytaert.