Posted by blond_and_stupid on August 23, 2006 at 4:54pm
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?
Comments
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.
where to write this ?
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 ;)
Thanks a lot...
Thank u guys..i was searching for this piece of code for long time..hope it will be useful for many users like me... :)
Vig@Amazech
Proper way of doing it
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:
<?phpfunction your_form($form_state) {
...
$form['#theme'] = 'your_theme_hook';
...
return $form
}
?>
<?phpfunction 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()
<?php/**
* Implements hook_theme().
*/
function your_theme_theme($existing, $type, $theme, $path) {
return array(
'your_theme_your_theme_hook' => array(
'arguments' => array('form' => NULL),
),
);
}
?>
http://www.jaypan.com/blog/th
http://www.jaypan.com/blog/themeing-drupal-6-forms-tables-checkboxes
Full-time freelancer, always looking for work.
jaypan.com (my portfolio)
Jay: Great tutorial! Here is
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-...