<?php
/**
* Implement hook_menu().
*/
function nai_test6_menu() {
$items = array();
$items['nai_test6/get_form'] = array(
'title' => 'Popup Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('ahah_demo_autocheckboxes_form'),
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
$items['ahah_demo/autocheckboxes/callback'] = array(
'page callback' => 'ahah_demo_autocheckboxes_callback',
//'page arguments' => array(2),
'access arguments' => array('access content'),
'type ' => MENU_CALLBACK,
);
return $items;
}
/**
* @file
* A Self-configure a form based on a select control
* Add the number of checkboxes specified in the select
*/
function ahah_demo_autocheckboxes_form(&$form_state) {
$default = !empty($form_state['values']['howmany']) ? $form_state['values']['howmany'] : 1;
//$form['howmany'] = array(
//'#title' => t('How many checkboxes do you want?'),
//'#type' => 'select',
//'#options' => array(1=>1, 2=>2, 3=>3, 4=>4),
//'#default_value' => $default,
//'#ahah' => array(
//'path' => 'ahah_demo/autocheckboxes/callback',
//'wrapper' => 'checkboxes',
//'effect' => 'fade',
//),
//);
// Add new tracks
$form['howmany'] = array(
'#type' => 'fieldset',
'#title' => t('Add another track'),
'#tree' => FALSE,
'#weight' => 1,
);
$form['howmany']['num_check_box'] = array(
'#type' => 'textfield',
'#title' => t('how many'),
'#weight' => 2,
);
// We name our button 'album_track_more' to avoid conflicts with other modules using
// AHAH-enabled buttons with the id 'more'.
$form['howmany']['add_more'] = array(
'#type' => 'submit',
'#value' => t('Add Options'),
'#weight' => 3,
//'#submit' => array('album_track_add_more_submit'),
'#default_value' => $default,
'#ahah' => array(
'path' => 'ahah_demo/autocheckboxes/callback',
'wrapper' => 'checkboxes',
'effect' => 'fade',
),
);
$form['checkboxes'] = array(
'#title' => t("Generated Checkboxes"),
'#prefix' => '
',
'#suffix' => '
',
'#type' => 'fieldset',
'#description' => t('This is where we get automatically generated checkboxes'),
'#weight' => 10,
);
$num_checkboxes = !empty($form_state['values']['num_check_box']) ? $form_state['values']['num_check_box'] : 1;
//for ($i=1; $i<=$num_checkboxes; $i++) {
//$form['checkboxes']['checkbox' . $i] = array(
//'#type' => 'checkbox',
//'#title' => 'Checkbox ' . $i,
//);
//}
//$num_checkboxes = 6;
$form['checkboxes']['options_list'] = array(
'#type' => 'checkboxes',
'#title' => 'Checkbox',
'#weight' => 11,
);
$form['checkboxes']['options_list']['#options'] = array();
for ($i = 1; $i <= $num_checkboxes; $i++) {
$form['checkboxes']['options_list']['#options'][5 . $i] = 'haha ' . $i;
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Click Me'),
'#weight' => 12,
);
return $form;
}
function ahah_demo_autocheckboxes_form_submit($form, &$form_state) {
file_put_contents('c:/test.txt', serialize($form_state));
}
/**
* Callback for autocheckboxes. Process the form with the number of checkboxes
* we want to provide
*/
function ahah_demo_autocheckboxes_callback() {
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// HACK: Select values changing never get recognized
unset ($form['howmany']['num_check_box']['#value']);
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$checkboxes = $form['checkboxes'];
$output = drupal_render($checkboxes);
// Final rendering callback.
print drupal_json(array('status' => TRUE, 'data' => $output));
exit();
}
?>
Another Version:
<?php
/**
* Implement hook_menu().
*/
function nai_test6_menu() {
$items = array();
$items['nai_test6/get_form'] = array(
'title' => 'Popup Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('ahah_demo_autocheckboxes_form'),
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
$items['ahah_demo/autocheckboxes/callback'] = array(
'page callback' => 'ahah_demo_autocheckboxes_callback',
//'page arguments' => array(2),
'access arguments' => array('access content'),
'type ' => MENU_CALLBACK,
);
return $items;
}
/**
* @file
* A Self-configure a form based on a select control
* Add the number of checkboxes specified in the select
*/
function ahah_demo_autocheckboxes_form(&$form_state) {
$default = !empty($form_state['values']['howmany']) ? $form_state['values']['howmany'] : 1;
//$form['howmany'] = array(
//'#title' => t('How many checkboxes do you want?'),
//'#type' => 'select',
//'#options' => array(1=>1, 2=>2, 3=>3, 4=>4),
//'#default_value' => $default,
//'#ahah' => array(
//'path' => 'ahah_demo/autocheckboxes/callback',
//'wrapper' => 'checkboxes',
//'effect' => 'fade',
//),
//);
// Add new tracks
$form['howmany'] = array(
'#type' => 'fieldset',
'#title' => t('Add another track'),
'#tree' => FALSE,
'#weight' => 1,
);
$form['howmany']['num_check_box'] = array(
'#type' => 'textfield',
'#title' => t('how many'),
'#weight' => 2,
);
// We name our button 'album_track_more' to avoid conflicts with other modules using
// AHAH-enabled buttons with the id 'more'.
$form['howmany']['add_more'] = array(
'#type' => 'submit',
'#value' => t('Add Options'),
'#weight' => 3,
//'#submit' => array('album_track_add_more_submit'),
'#default_value' => $default,
'#ahah' => array(
'path' => 'ahah_demo/autocheckboxes/callback',
'wrapper' => 'checkboxes',
'effect' => 'fade',
),
);
$form['checkboxes'] = array(
'#title' => t("Generated Checkboxes"),
'#prefix' => '',
'#suffix' => '',
'#type' => 'fieldset',
'#description' => t('This is where we get automatically generated checkboxes'),
'#weight' => 10,
);
$num_checkboxes = !empty($form_state['values']['num_check_box']) ? $form_state['values']['num_check_box'] : 1;
//for ($i=1; $i<=$num_checkboxes; $i++) {
//$form['checkboxes']['checkbox' . $i] = array(
//'#type' => 'checkbox',
//'#title' => 'Checkbox ' . $i,
//);
//}
//$num_checkboxes = 6;
$form['checkboxes']['options_list'] = array(
'#type' => 'checkboxes',
'#title' => 'Checkbox',
'#weight' => 11,
);
$form['checkboxes']['options_list']['#options'] = array();
for ($i = 1; $i <= $num_checkboxes; $i++) {
$form['checkboxes']['options_list']['#options'][5 . $i] = 'haha ' . $i;
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Click Me'),
'#weight' => 12,
);
return $form;
}
function ahah_demo_autocheckboxes_form_submit($form, &$form_state) {
file_put_contents('c:/test.txt', serialize($form_state));
}
/**
* Callback for autocheckboxes. Process the form with the number of checkboxes
* we want to provide
*/
function ahah_demo_autocheckboxes_callback() {
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
// HACK: Select values changing never get recognized
unset ($form['howmany']['num_check_box']['#value']);
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$checkboxes = $form['checkboxes'];
$output = drupal_render($checkboxes);
// Final rendering callback.
print drupal_json(array('status' => TRUE, 'data' => $output));
exit();
}
?>
No comments:
Post a Comment