I'm using time_tracker_simple module in Drupal. When someone selects from the drop down list and hits submit it stays on the same page but the value is not retain. How can I retain the values for the drop down list when selected.
Below is the code used in the module.
function time_tracker_simple_timer_form($form_state) {
global $user;
$active_timer = _time_tracker_simple_get_timer();
//*** Both start and start buttons must actually be on the form for #submit to work correctly
$form['stop'] = array(
'#value' => t('Stop'),
'#type' => 'submit',
'#weight' => 20,
'#access' => FALSE,
'#button_type' => 'timer_control_stop',
'#submit' => array('time_tracker_simple_timer_stop'),
);
$form['start'] = array(
'#value' => t('Start'),
'#type' => 'submit',
'#access' => FALSE,
'#weight' => 20,
'#submit' => array('time_tracker_simple_timer_start'),
'#button_type' => 'timer_control_start',
);-----This part is the drop down list------
$activities = array();
$activities[] = '(none)';
$results = db_query("SELECT * FROM {time_tracker_activity} ORDER BY weight ASC");
while ($result = db_fetch_object($results)) {
$activities[$result->taid] = $result->name;
}
if (count($activities) > 1) {
// The activity choser
$form['activity'] = array(
'#title' => t('Activity'),
'#type' => 'select',
'#weight' => 5,
'#options' => $activities,
);
}
the drupal form api allows you to pass a default value to the form item like this:
$form['activity'] = array(
'#title' => t('Activity'),
'#type' => 'select',
'#weight' => 5,
'#options' => $activities,
'#default_value' => $form_state['post']['activity']
);
Related
I have a form with a multiple select field where a user can select several values in it
$form['export_type_section']['export_type'] = array(
'#title' => t('Export types'),
'#type' => 'select',
'#multiple' => TRUE,
'#size' => 10,
'#options' => $options,
'#ajax' => array(
'event' => 'change',
'callback' => 'test_export_form_export_type_ajax_callback',
),
);
I have another field in the form
$form['document_type'] = array(
'#type' => 'select',
'#options' => array(0 => t('All Types'), 1 => t('Test1'), 2 => t('Test2'), 3 => t('Test3')),
'#title' => 'Document Types',
'#default' => 0,
'#ajax' => array(
'event' => 'change',
'callback' => 'test_export_form_document_type_ajax_callback',
),
);
I am trying to figure out how to reset the export_type field when the document_type is changed. I would like to clear the selected values so that the user can reselect what is needed whenever the document_type changes. I tried several formats in the test_export_form_document_type_ajax_callback but none worked.
Anybody knows how to do it?
I have created a form in drupal. I don't know how to handle the submission. I want to do some selection from database with the values i get from form. Here is my code to create form
function q_search_form() {
$form['qsearch']['category'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Automotive', 2 => 'Real Estate'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<table width="470"><tr><td width="170">Select Category</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['city'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Calicut', 2 => 'Kochi'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">City</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['property'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'House', 2 => 'Land'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Property</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['wanto'] = array(
'#type' => 'select',
'#options' => array(0 => 'Any', 1 => 'Sell', 2 => 'Buy'),
'#attributes' => array('class' => 'drop-box'),
'#prefix' => '<tr><td width="170">Want to</td><td width="300">',
'#suffix' => '</td></tr>'
);
$form['qsearch']['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
'#attributes' => array('id' => 'Search', 'class' => 'srch-button'),
'#prefix' => '<tr><td><a class="adv-srch" href="#">Advance Search</a></td><td>',
'#suffix' => '</td></tr></table>'
);
return $form;
}
You can write following submit function,
function q_search_form_submit($form, &$form_state) {
// To get selected values from form
$values = $form_state['values'];
// do print_r($values); exit; to check the values
}
This is pretty simple. You just have to implement the form_submit function. From the function above, I assume the name of your module to be q_search. So, here is what the submit function would look like:
function q_search_form_submit($form, &$form_state) {
// you can get the values submitted by the users using
// `$form_state['values']`, and use the database functions
// to implement your logic.
}
If you also want to validate the user inputs before the actual submit, you should add a validation function which would look like:
function q_search_form_validate($form, &$form_state) {
// validate here
}
You can use form_set_error, in the validate function if the validation fails.
You can do pretty much anything you want in the form_submit function.
function q_search_form_submit($form, &$form_state) {
$category = $form_state['values']['category'];
$city = $form_state['values']['city'];
//etc..
//use these values to query your database tables
$query = db_select($category, 'c');
//narrow results to those only in the selected city:
$query->join($city, 'cy', 'c.city = cy.cid');
//narrow results further with more ->join statements
$results = $query
->fields('c', array(fields you want from the categories table))
//fields from other tables...
->execute();
//do whatever with the $results (print on a different page, print in a table, etc).
I create a form like this in .module file:
function form_registration_form($form, &$form_state) {
$form['registration']['email'] = array(
'#title' => t('EMAIL ADDRESS'),
'#type' => 'textfield',
'#required' => TRUE,
'#size' => 44,
'#maxlength' => '80',
'#rules' => array(
'email',
'length[10, 50]',
)
);
$form['registration']['password'] = array(
'#title' => t('PASSWORD'),
'#type' => 'password',
'#required' => TRUE,
'#size' => 44,
'#maxlength' => '80',
);
$form['registration']['submit'] = array(
'#value' => 'SIGN IN',
'#type' => 'submit',
'#submit' => array('form_registration_handler')
);
return $form;
}
function form_registration_handler($form, &$form_state){
$email = $form_state['registration']['email'];
drupal_set_message($email);
}
However drupal always say that "Undefined index: registration in form_registration_form_submit()". I really dont know what I am doing wrong here. Any suggestions will be very useful for me. Thank you very much.
You can always enable the Devel module and dsm($form) the submit function. For instance:
function form_registration_handler($form, &$form_state){
dsm($form);
dsm($form_state);
}
By DSMing, you can easily find the value you want to use in your submit function.
Add this lines to head of your form_registration_form function:
$form = array();
$form['registration'] = array();
The notice appear becuse $form['registration'] is not declared as a void array.
Submit button appears after selecting from the select list drop down ,
Can anyone help me understand why it is not working?
$form['user_fields']['optinal_packages'] = array(
'#type' => 'select',
'#title' => t('Optional Packages'),
'#options' => $packages_array,
//'#weight' => 15,
'#description' => t('Please press the "Push" button to update device package.'),
'#default_value' => -1,
);
$form['user_fields']['push'] = array(
'#type' => 'submit',
'#value' => t('Push'),
// '#weight' => 16,
'#prefix' => '<div id="phone_user_push_package">',
'#suffix' => '</div>',
'#states' => array(
'visible' => array( // Action to take: Make visible.
//':input[name="optinal_packages"]' => array('!value' => '-1'),
'select[name="optinal_packages"]' => array('!value' => '-1'),
),
),
);
Thanks
dana
Change the code to be
$form['user_fields']['push'] = array(
'#type' => 'submit',
'#value' => t('Push'),
// '#weight' => 16,
'#prefix' => '<div id="phone_user_push_package">',
'#suffix' => '</div>',
'#states' => array(
'invisible' => array( // edited line
':select[name="optinal_packages"]' => array('value' => '-1'), // edited line
),
),
);
Change select[name="optinal_packages"] to be :select[name="optinal_packages"]. If it doesn't work, try to change the selector to be CSS-like #edit-optinal-packages.
Hence the code will be:
'#edit-optinal-packages' => array('value' => '-1'),
I am creating a wizard from the Drupal example file and would like to refactor the segments of code that are repeated when setting up items like options and radios.
I have already tried a simple function passing "ordinary" and "preferential" but can't find a way to make it work.
Can someone give me an idea of the best way to do this?
unfactored code is as below:
function services_wizard_share_capital_classes($form, &$form_state) {
$form['share_classes']['type_of_class'] = array(
'#type' => 'select',
'#title' => t('What type of share will this class be?'),
'#options' => array(
1 => t('Ordinary'),
2 => t('Preferential'),
),
);
$form['ordinary']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Ordinary Shares"),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => '1'),
),
),
);
$form['preferential']['share_type'] = array(
'#type' => 'item',
'#description' => t("You chose Preferential Shares"),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => '2'),
),
),
);
return $form;
}
I'm not sure about this being the best way but it's certainly a way to refactor your code:
function _services_wizard_share_capital_classes_add_el(&$form, $name, $description, $index) {
$form[$name]['share_type'] = array(
'#type' => 'item',
'#description' => t($description),
'#states' => array(
'visible' => array(
':input[name="type_of_class"]' => array('value' => "$index"),
),
)
);
}
function services_wizard_share_capital_classes($form, &$form_state) {
// Other code
_services_wizard_share_capital_classes_add_el($form, 'ordinary', 'You chose Ordinary Shares', 1);
_services_wizard_share_capital_classes_add_el($form, 'preferential', 'You chose Preferential Shares', 2);
// etc...
}