drupal 7 ajax framework example does not work - drupal-7

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'];
}

Related

how to get the submitted value of tableselect in 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']);
}
}

form submission in Drupal

i have made a custom module for form in drupal using dupal API,and made a table in my database
What i want is to save the value of drop down box instead of it's index into database...my .module file is:
<?php
function form_test_menu() {
$items['formtest'] = array(
'title' => 'Form Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_form'),
'access callback' => TRUE,
);
return $items;
}
function form_test_form($form,&$form_submit) {
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['college'] = array(
'#title' => t('College/University'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['education'] = array(
'#title' => t('Education'),
'#type' => 'select',
'#description' => 'Select your higher education .',
'#options' => array(t('--- SELECT ---'), t('B.tech'), t('MCA'), t('MBA'),t('Graduate')),
'#required' => TRUE,
);
$form['percentage'] = array(
'#title' => t('Percentage/CGPA'),
'#type' => 'textfield',
'#required' => TRUE,
);
$form['application'] = array(
'#title' => t('Job Applied'),
'#type' => 'select',
'#options' => array(t('---select---'),t('Web Developer'),t('Web Designer'),t('SEO'),t('Marketing')),
'#required' => TRUE,
);
$form['submit'] = array(
'#value' => 'Submit',
'#type' => 'submit',
);
return $form;
}
function form_test_form_submit($form, $form_state) {
global $user;
// Here u can insert Your custom form values into your custom table.
db_insert('drupal')
->fields(array(
'name' => $form_state['values']['name'],
'college' => $form_state['values']['college'],
'education' => $form_state['values']['education'],
'percentage' => $form_state['values']['percentage'],
'application' => $form_state['values']['application'],
))->execute();
drupal_set_message("successfully saved data");
}
?>
All are in one file(.module)....any help will be appreciated.
Thank you.
The solution is to pass options array from one function to another. To achieve that use the second argument of "form_test_form" and "form_test_form_submit" functions.
function form_test_form($form,&$form_submit) {
$form_submit['myoptions'] = array(' select', 'option1' .....
}
Then
function form_test_form_submit($form,&$form_state) {
$myoptions = $form_state['myoptions']; etc ...
}

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 #default_value not applied in select option in drupal_render

I try to print a drupal 'select option' element in a form .I think drupal_render not applying #default_value.every thing is ok except #default_value not applied.
where is the problem?anybody know how i can do this?
do #default_value accept string value?
this is pseudo of my codes:
function test_menu(){
$items=array();
$items['admin/config/regional/test']=array(
'title' => 'test',
'description' => t('test'),
'page callback' =>'drupal_get_form',
'page arguments' => array('test_function'),
);
$items[]=array();
return $items;
}
function test_function(){
$header = array
(
'test1' => t('test1'),
'test2'=> t('test2'),
);
$a=(1,2,3);
$$options=array();
foreach($a as $i=>$v)
{
$f['type'] = array(
'#type' => 'select',
'#options' => array(1,2,3,4),
'#default_value'=>1,
);
$options += array($name=>array( 'test1' => $v,
'test2'=> drupal_render($f['type']) ,
}
$form['table'] = array
(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE
//'#empty' => t('No users found'),
);
$form['submit'] = array
(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
I test textfield but its also not work and not accept #default_value in drupal_render
$f['test3']=array(
'#type'=>'textfield',
'#title'=>'test3',
'#default_value' =>'aaa',
);
I suppose this is beacuse using drupal_render .anybody have a solution?
In Drupal_render 's used in drupal_get_form , #default_value not set use must use #value instaed of it.
$f['type'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array('1','2','3','4')),
'#value'=> '1',
);
The following code doesn`t work:
$form['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
$form['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
return $form;
But then i did the following:
$form['group'] = array('#tree' => TRUE);
$form['group']['title'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
$form['group']['title1'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array(t('opt1'), t('opt3'), t('opt4'), t('opt5'), t('opt6'))),
'#default_value' => array($default_value_key));
return $form;
And default values now works.
I have the same problem. finally, i found one method. As you said, the default_value does not work. So make the default_value fixed as 0. and change the options array, put the default value on the top.
If you look at the example from Drupal's Form API, you'll see that the #options setting takes a key-value pair array and in the #default_value, you should specify the key of the default value, not the string value.
Also, according to documentation for the #options setting, #options is expecting String values. So your select should be more like:
$f['type'] = array(
'#type' => 'select',
'#options' => drupal_map_assoc(array('1','2','3','4')),
'#default_value'=> '1',
);

Resources