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
Lasy to search for examples, just can point to check '#prefix' and '#suffix' options.
Or, better to produce table with theme('table', )
Examples?!
I tried using #prefix and #suffix without success.
Where can one find easy-to-understand examples?
-
Theming forms
theme_table (use:
theme('table', $header, $rows, $attributes, caption);)still puzzled...
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...
<?phpecho "<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
Writing forms in tables
Finally!
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
Hi,
Thank you for posting this code.
I was searching for some way to display my form elements in a table.
Thanks again.