how to get the submitted value of tableselect in drupal 7 - drupal-7

function test($form, &$form_state){
$form = array();
$header = array(.............);
$values = array(.............);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => $IsCheckbox,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state){
$selected = $form_state['values']['table'];
drupal_set_message($selected) // displays array index (0,1,2 etc)
return;
}
How to get the selected table row values in Drupal form. Need assistance on the issue. Any help would be appreciated.

What you get in your $selected is the index of $rows that you have selected in your table. To get the values in $rows you need to use the index that you have in $selected.
I created an easy example how to do it here:
function test($form, &$form_state)
{
$form = array();
$header = array(
'first_name' => t('First Name'),
'last_name' => t('Last Name'),
);
$rows = array(
// These are the index you get in submit function. The index could be some unique $key in database.
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => true,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state)
{
// This function should not be duplicated like this but It was easier to do.
$rows = array(
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$names = array();
// Remove the names that has not been checked
$selected_names = array_filter($form_state['values']['table']);
// Iterate over the indexes that was selected to get the data from original array
foreach ($selected_names as $index ) {
array_push($names, $rows[$index]);
}
foreach($names as $name) {
drupal_set_message($name['first_name'] . ' ' . $name['last_name']);
}
}

Related

drupal 7 ajax framework example does not work

I want to add one form in user profile page. So i created user-profiled.tpl.php. In that template,i just copied the example from ajax framework for testing. Here is the whole codes:
function ajax_example_simplest($form, &$form_state) {
$form = array();
$form['changethis'] = array(
'#type' => 'select',
'#options' => array(
'one' => 'one',
'two' => 'two',
'three' => 'three',
),
'#ajax' => array(
'callback' => 'ajax_example_simplest_callback',
'wrapper' => 'replace_textfield_div',
),
);
$form['replace_textfield'] = array(
'#type' => 'textfield',
'#title' => t("The default value will be changed"),
'#description' => t("Say something about why you chose") . "'" .
(!empty($form_state['values']['changethis'])
? $form_state['values']['changethis'] : t("Not changed yet")) . "'",
'#prefix' => '<div id="replace_textfield_div">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_example_simplest_callback($form, $form_state) {
return $form['replace_textfield'];
}
I am confusing now. After i clicked the checkbox, nothing change in textfield. My jquery version is 1.9. Is there any other constraints?
The change is in description and not in textfield(I did the same error first as you did). However I have updated the code so it also shows the value in 'textfield'.
function ajax_example_simplest($form, &$form_state)
{
$form = array();
$form['changethis'] = array(
'#type' => 'select',
'#options' => array(
'one' => 'one',
'two' => 'two',
'three' => 'three',
),
'#ajax' => array(
'callback' => 'ajax_example_simplest_callback2',
'wrapper' => 'replace_textfield_div',
),
);
$number = (!empty($form_state['values']['changethis']) ? $form_state['values']['changethis'] : t("Not changed yet"));
$form['replace_textfield'] = array(
'#type' => 'textfield',
'#title' => t("The default value will be changed"),
// Not that value was changed in the description
'#description' => t("Say something about why you chose") . " '" . $number . "'",
// Also add next line if you want to see it in the text box
'#value' => $number,
'#prefix' => '<div id="replace_textfield_div">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_example_simplest_callback2($form, $form_state) {
return $form['replace_textfield'];
}

Drupal7 Radio button value to datasabe is not working

Adding textbox ,radiobuttons and submit button is working fine..
enter code here:
' $form = array();
$form['name'] = array(
'#title' => 'Your Name',
'#type' => 'textfield',
'#size' => '30',
);
$active = array(0 => t('Blog-Url'), 1 => t('Twitter-Name'));
$form['settings']['active'] = array(
'#type' => 'radios',
'#title' => t('Enter nature of the name'),
'#default_value' => isset($node->active) ? $node->active : 1,
'#options' => $active,
'#description' => t('Enter nature of the name, and characteritics.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;'
// When Submit button is clicked the action take place and insert into database not working..
` function sform_add_form_submit($form,$form_state){
$name = trim($form_state['values']['name']);
$formwem= $form_state['values']['settings']['active'];
$query = "INSERT INTO twittername (`name`,'Blogurt','Twitter_name') VALUES ('%s','%s','%s')";
$result = db_query($query, $name, $formwem ); `
Please help me ....
Try re-writing your query to:
$name = trim($form_state['values']['name']);
$formwem = $form_state['values']['settings']['active'];
$result = db_query("INSERT INTO twittername (name, Blogurt, Twitter_name) VALUES (':name',':blogurt',':twitter_name')", array(':name' => $name, ':blogurt' => $formwem, ':twitter_name' => $formwem));
Though I'm not sure which form field populates the two columns Blogurt and Twitter_name. Can you please explain?

drupal 7 ajax form rebuild form to change form_state value

I have one drupal form, i want it works as "add one more": there is one button at the end of the form, when click it, another form will append to it. Here is part of my code:
function add_passenger_form($form, &$form_state){
if(!isset($form_state['num_names'])){
$form_state['num_names']=2;
}
$form['add_button']= array(
'#type' => 'button',
'#value'=> 'Add more',
'#ajax' => array(
'callback' => 'ajax_add_passenger_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'last_field',
),
);
$form['form_number']=array(
'#prefix'=>'<div class="form_number">',
'#suffix'=>'</div>',
'#type'=>'hidden',
'#value'=>2,
);
}
function ajax_add_passenger_callback($form, &$form_state){
$form_state['num_names']++;
$form_state['rebuild']=TRUE;
$form['form_number']['#value']=$form_state['num_names'];
return array(
'#type' => 'ajax',
'#commands' => array(
ajax_command_replace(".form_number", render($form['form_number'])),
)
);
}
I want the $form['num_names']['#value'] increase one when i click the add one button. Now it only works once (to 3).
Change your code like this.. this will create button "Add more" to add new passenger and one field (name) for each passenger. You can replace that "Name" field for example with fieldset and add more fields for one passenger.
function add_passenger_form($form, &$form_state){
if(!isset($form_state['num_names'])){
$form_state['num_names']=2;
}
// Create wrapper for ajax
$form['#prefix'] = '<div id="form-wrapper">';
$form['#suffix'] = '</div>';
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['name_' . $i] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#required' => true
);
}
$form['add_button']= array(
'#type' => 'button',
'#value'=> 'Add more',
'#ajax' => array(
'callback' => 'ajax_add_passenger_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'form-wrapper',
),
);
}
function ajax_add_passenger_callback($form, &$form_state){
$form_state['num_names']++;
// rebuild whole form with new values
$form_state['rebuild'] = true;
}
Also check this link for more info about ajax:
https://api.drupal.org/api/examples/ajax_example!ajax_example_graceful_degradation.inc/7
function add_passenger_form($form, &$form_state){
$form['#tree'] = TRUE;
if(empty($form_state['num_names'])){
$form_state['num_names'] = 2;
}
$form['passenger_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('List of Passengers'),
'#prefix' => '<div id="passenger-form-wrapper">',
'#suffix' => '</div>',
);
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['passenger_fieldset']['passenger_name'][$i] = array(
'#type' => 'textfield',
'#title' => t('Passenger Name'),
'#required' => false,
);
}
$form['passenger_fieldset']['add_button']= array(
'#type' => 'submit',
'#value'=> t('Add passenger'),
'#submit' => array('add_passenger_form_add_one'),
'#ajax' => array(
'callback' => 'add_passenger_form_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'passenger-form-wrapper',
),
);
// Remove passenger
if ($form_state['num_names'] > 2) {
$form['passenger_fieldset']['remove_button'] = array(
'#type' => 'submit',
'#value' => t('Remove Passenger'),
'#submit' => array('remove_passenger_form_remove_one'),
'#ajax' => array(
'callback' => 'add_passenger_form_callback',
'method' => 'replace',
'effect' => 'fade',
'wrapper' => 'passenger-form-wrapper',
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/*
* add a textfield one more
*/
function add_passenger_form_add_one($form, &$form_state) {
$form_state['num_names']++;
// rebuild whole form with new values
$form_state['rebuild'] = true;
}
function remove_passenger_form_remove_one($form, &$form_state) {
if ($form_state['num_names'] > 2) {
$form_state['num_names']--;
}
$form_state['rebuild'] = TRUE;
}
function add_passenger_form_callback($form, $form_state){
return $form['passenger_fieldset'];
}

Drupal 7 Forms API - AJAX Forms error: An illegal choice has been detected. Please contact the site administrator

I have a form set up where a users selects an item from a drop down. Once that item is selected another drop down is populated. Then depending on the value that is selected from the second drop down a fieldset may or may not be shown. If the field set is shown there is a field and a button. By clicking the button you add another copy of the same field. Once there is more than one a remove one button shows up as well. I got the basis for the code from here:
http://api.drupal.org/api/examples/ajax_example%21ajax_example_graceful_degradation.inc/function/ajax_example_add_more/7
The problem is when I click 'Add Another Survey Question' the first time it works fine and add a field. When I click it a second time or when I click remove now that there are two, I get this error: 'An illegal choice has been detected. Please contact the site administrator.'
What am I doing wrong?
Here's my code:
function touchpoints_addmetric_form($form, &$form_state, $tp_id) {
$selectedType = isset($form_state['values']['type']) ? $form_state['values']['type'] : FALSE;
$types = db_query('SELECT * FROM {touchpoints_metric_types}') -> fetchAllKeyed(0, 1);
$form['#tree'] = TRUE;
$form['type'] = array(
'#type' => 'select',
'#title' => t('Metric Type'),
'#options' => $types,
'#required'=>TRUE,
'#ajax' => array(
'event' => 'change',
'wrapper' => 'method-wrapper',
'callback' => 'touchpoints_method_callback'
)
);
$form['measurementmethod'] = array(
'#type' => 'select',
'#title' => t('Measurement Method'),
'#required'=>TRUE,
'#prefix' => '<div id="method-wrapper">',
'#suffix' => '</div>',
'#options' => _get_methods($selectedType)
);
$form['survey'] = array('
#type' => 'fieldset',
'#collapsible' => FALSE,
'#states' => array(
'visible' => array(
array(
array(':input[name="measurementmethod"]' => array('value' => '5')),
'xor',
array(':input[name="measurementmethod"]' => array('value' => '6')),
'xor',
array(':input[name="measurementmethod"]' => array('value' => '7'))
)
)
)
);
$form['survey']['contents'] = array(
'#type' => 'fieldset',
'#collapsible' => FALSE,
'#prefix' => '<div id="survey-div">',
'#suffix' => '</div>',
);
if (empty($form_state['num_surveys'])) {
$form_state['num_surveys'] = 1;
}
for ($i = 1; $i <= $form_state['num_surveys']; $i++) {
$form['survey']['contents']['survey_question'][$i] = array(
'#type' => 'textfield',
'#title' => t('Survey Question ' . $i),
'#size' => 70, '#maxlength' => 100,
);
}
$form['survey']['contents']['addsurvey'] = array(
'#type' => 'submit',
'#value' => t('Add Another Survey Question'),
'#submit' => array('touchpoints_metrics_survey_add_one'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'touchpoints_metrics_survey_callback',
'wrapper' => 'survey-div',
),
);
if ($form_state['num_surveys'] > 1) {
$form['survey']['contents']['removesurvey'] = array(
'#type' => 'submit',
'#value' => t('Remove A Survey Question'),
'#submit' => array('touchpoints_metrics_survey_remove_one'),
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'touchpoints_metrics_survey_callback',
'wrapper' => 'survey-div',
),
);
}
return $form;
}
function _get_methods($selected) {
if ($selected) {
$methods = db_query("SELECT * FROM {touchpoints_m_methods} WHERE mt_id=$selected") -> fetchAllKeyed(0, 2);
} else {
$methods = array();
}
return $methods;
}
function touchpoints_method_callback($form, &$form_state) {
return $form['measurementmethod'];
}
function touchpoints_metrics_survey_add_one($form, &$form_state) {
$form_state['num_surveys']++;
$form_state['rebuild'] = TRUE;
}
function touchpoints_metrics_survey_remove_one($form, &$form_state) {
if ($form_state['num_surveys'] > 1) {
$form_state['num_surveys']--;
}
$form_state['rebuild'] = TRUE;
}
function touchpoints_metrics_survey_callback($form, &$form_state) {
return $form['survey']['contents'];
}
I have encountered this error a few times, and this is what I found:
This is Drupal's way of saying "Hey, you tried to submit this form with a , checkbox or radio button option that wasn't included in the original form definition! That's not allowed." - See more at: http://proofgroup.com/blog/2008/jul/debugging_mysterious_illegal_choice_has_been_detected_please_contact_site_administrato#sthash.vDNmqslL.dpuf
Instead of custom code, maybe you should consider module Conditional Fields
Or a work around: '#validate' => TRUE source
Actually, it's '#validated' => TRUE.

drupal admin.inc, loop list_modules as form settings

I need to loop the values of this
$blocks = array();
$blocks['list_modules']
with this:
$form['advanced']['custom_acc'] = array(
'#type' => 'select',
'#title' => t('module title'),
'#options' => array(
0 => t('No control'),
1 => t('Controlled'),
),
);
so it should become something like this
while (list(, $value) = each($blocks)) {
echo "
$form['advanced']['custom_->blocks array value'] = array(
'#type' => 'select',
'#title' => t('->blocks array value'),
'#options' => array(
0 => t('No control'),
1 => t('Controlled'),
),
);
";
}
but of course this is not working, got any ideas? I guess I have to use an foreach, I hope you can see what Im trying to achieve,
thanks in advance!
fixed it myself :)
$blocks = array();
$blocks = module_list();
foreach ($blocks as $key => $title) {
$form['advanced'][$title] = array(
'#type' => 'select',
'#title' => t($title),
'#options' => array(
0 => t('No control'),
1 => t('Controlled'),
),
);
}

Resources