By newbuntu on
I stumbled on this link http://blog.riff.org/2008_08_12_checkboxes_forms_step_step . It's a quick demo module that touches on several aspects of the form API.
I have seen many discussions regarding the benefits of form API. I implemented an equivalent module without using the form API. I'd like to get feedback from developers to compare these two approaches.
The original link uses drupal 5. If want test on 6, you need to modify the hook_menu()
Thank you for your time!
function form_demo_menu($may_cache) {
if ($may_cache) {
$items[] = array (
'path' => 'form_demo',
'callback' => 'form_demo_form',
'access' => user_access('access content'),
'title' => t('Form demo'), );
}
return $items;
}
function form_demo_form() {
$options = array('a', 'b', 'c');
foreach($options as $op) {
variable_set($op, '');
if ($_POST['fd_boxes']) {
foreach($_POST['fd_boxes'] as $abox){
if ($abox == $op)
variable_set($op, 'checked');
}
}
}
$out .= "<form method='post'>";
foreach($options as $op)
$out .= "<input type='checkbox' name='fd_boxes[]' value='$op' ".variable_get($op, '').">$op<br>";
$out .= "<input type=submit></form>";
return $out;
}
Comments
For those who wonder how the
For those who wonder how the form looks like, it looks somewhat like the following on the screen:
as you click on each box and submit, it remembers which box is checked or unchecked.