Obtain form value from Drupal 6 module form while loop - php

inside form hook:
$count = 1;
while($slice = db_fetch_array($fruit)){
$section = $slice['section'];
$form[$section][$count] = array(
'#type' => $slice['type'],
'#title' => $slice['title'],
'#value' =>$slice['value'],
//'#default_value' => $slice['default_value'],
'#disabled' => $true_statement,
'#size' => $slice['size'],
'#description' => $slice['description'],
'#options' => unserialize($slice['options']),
'#prefix' => $slice['prefix'],
'#suffix' => $slice['suffix'],
);
$count = $count+1;
}
inside save :
function student_grant_save($form, &$form_state) {
$vari = $form_state['values']['2']; //or question count # 3,4,5...etc...
drupal_set_message(t('hi').$vari);
}
only the hi gets printed. Why can't i see the form value that the user imputed?
i used dpm($form_state); and I see that i have value entries but they are blank even though user entered them in. For some reason the 'value' entry in my mysql table that has all the question atributes, is taking over the 'default_value' and not letting the user alter the form values upon submit. Is there any way around this?

Related

How to get a value from a form defined in one function, using a submit button defined in another function? - Drupal 7.x

I'm having problems displaying a textfield form value in drupal_set_message on clicking a form button which has been defined in another function in Drupal 7.x.
My First Function contains the Form textfield only (I don't want a 'Submit' button in the first function cause of the way I want to display the form UI separately on a page)
function firstformfun_form($form, &$form_state) {
$form['name'] = array(
'#title' => t(''),
'#type' => 'textfield',
'#attributes' =>array('placeholder' => t('test')),
);
return $form;
}
The Other function consisting of other forms and a 'Submit' button:
function firstformfuntwo_form($form, &$form_state) {
$form['name'] = array(
'#title' => t(''),
'#type' => 'textfield',
'#attributes' =>array('placeholder' => t('test')),
);
$form['submitbtn'] = array(
'#title' => t('Submit'),
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('submit_name'),
);
return $form;
}
I've defined a function for the 'Submit' button form as below and want it to fetch the value from the form 'Name' (textfield) in function 'firstformfun_form':
function submit_name($form, &$form_state) {
$output = $form_state['values']['name'];
drupal_set_message('Name ' .$output);
return $page;
}
The result of the same only displays 'Name' in a drupal message box, but doesn't display the output value from the 'Name' text box. Could someone kindly guide me on this?
Thanks

How to display related data in dropdown list by selecting specific data from corresponding dropdown list

I am using Drupal7,in that i have module where in i have written my own functions using php,with two dropdown list in row, now my requirement is when i select for example say "A" from 1st drop down then in 2nd drop down only related to "A" all data need show in the second dropdown,
In simple words if i select the city name from the 1st dropdown, then all the colleges of that city should display in 2nd dropdown.How can i achieve this in php?i found this link and piece of code http://w3shaman.com/article/creating-ajax-dropdown-drupal-7 but no use, it will be great helpful for me if someone answer for this.
If you created your form using Drupal Form API, then you can achive this using the Drupal Ajax.
Below is a code snippet from the Drupal Ajax examples module
function ajax_example_dependent_dropdown($form, &$form_state) {
// Get the list of options to populate the first dropdown.
$options_first = _ajax_example_get_first_dropdown_options();
// If we have a value for the first dropdown from $form_state['values'] we use
// this both as the default value for the first dropdown and also as a
// parameter to pass to the function that retrieves the options for the
// second dropdown.
$selected = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);
$form['dropdown_first'] = array(
'#type' => 'select',
'#title' => 'Instrument Type',
'#options' => $options_first,
'#default_value' => $selected,
// Bind an ajax callback to the change event (which is the default for the
// select form type) of the first dropdown. It will replace the second
// dropdown when rebuilt.
'#ajax' => array(
// When 'event' occurs, Drupal will perform an ajax request in the
// background. Usually the default value is sufficient (eg. change for
// select elements), but valid values include any jQuery event,
// most notably 'mousedown', 'blur', and 'submit'.
// 'event' => 'change',
'callback' => 'ajax_example_dependent_dropdown_callback',
'wrapper' => 'dropdown-second-replace',
),
);
$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => $options_first[$selected] . ' ' . t('Instruments'),
// The entire enclosing div created here gets replaced when dropdown_first
// is changed.
'#prefix' => '<div id="dropdown-second-replace">',
'#suffix' => '</div>',
// When the form is rebuilt during ajax processing, the $selected variable
// will now have the new value and so the options will change.
'#options' => _ajax_example_get_second_dropdown_options($selected),
'#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function ajax_example_dependent_dropdown_callback($form, $form_state) {
return $form['dropdown_second'];
}
function _ajax_example_get_second_dropdown_options($key = '') {
$options = array(
t('String') => drupal_map_assoc(
array(
t('Violin'),
t('Viola'),
t('Cello'),
t('Double Bass'),
)
),
t('Woodwind') => drupal_map_assoc(
array(
t('Flute'),
t('Clarinet'),
t('Oboe'),
t('Bassoon'),
)
),
t('Brass') => drupal_map_assoc(
array(
t('Trumpet'),
t('Trombone'),
t('French Horn'),
t('Euphonium'),
)
),
t('Percussion') => drupal_map_assoc(
array(
t('Bass Drum'),
t('Timpani'),
t('Snare Drum'),
t('Tambourine'),
)
),
);
if (isset($options[$key])) {
return $options[$key];
}
else {
return array();
}
}

How to create array of all the language in drupal form dropdown list

I need to create dropdown list for all languages in my Drupal form, Is there any function in PHP to achieve this. I want to create an array of all languages and pass it to drupal form.
function video_upload_subtitles_form($form, &$form_state) {
$form = array('#attributes' => array('enctype' => 'multipart/form-data'));
$lang_list = array();//**how to create this array**
$form['video_name'] = array(
'#title' => t('Name Of the video'),
'#type' => 'textfield',
);
$form['sub_file'] = array(
'#type' => 'file',
'#title' => t('Upload video'),
'#description' => t('Pick a video file to upload.'),
);
$form['user_list']=array(
'#type'=>'select',
'#title' => t('Language'),
'#options' => $lang_list,//array of language
'#multiple' => false,
'#attributes'=>array('size'=>4),
'#weight'=>8,
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
I know listing of language can be done in php by listing and making a array manually but is it possible to do with any php function, just looking for some easy way to save time.
Got this cool website for Country List . Is there anything for the Language like this
Try this,
$select = db_select('Your table name', 's');
$select = $select->fields('s',array('languages_name','id'));
// 'languages_name' , 'id' this is a column
$queried_nodes = $select->execute()
->fetchAllAssoc('id');
$lang_list = array();
foreach ($queried_nodes as $result)
{
$lang_list[$result->languages_name] = t($result->languages_name);
}
after set a $lang_list variable
$form['user_list']=array(
'#type'=>'select',
'#title' => t('Language'),
'#options' => $lang_list,//array of language
'#multiple' => false,
'#attributes'=>array('size'=>4),
'#weight'=>8,
);
you are create a new Language table or content types and you use a drupal inbuilt language list so try this code.
include_once DRUPAL_ROOT . '/includes/iso.inc';
$lang = _locale_get_predefined_list();
$lang_list = array();
foreach ($lang as $key => $value)
{
$lang_list[$value[0]] = t($value[0]);
}
after use a $lang_list varible in form api
The easiest way is probably to put all the languages you want in a table in your DB.
Then you could just use the following code to create your select box:
$languages = db_query("SELECT id, name FROM languages")->fetchAllKeyed();
$form['language'] = array(
'#type' => 'select',
'#title' => t('Choose language'),
'#options' => $languages,
);
To populate your table of languages you can download a csv here.

How to retain and retrieve image details in Drupal 7 Ajax Form with Add More option?

My custom module code:
<?php
function my_module_menu() {
$items = array();
$items['form-example'] = array(
'title' => 'My Module Form',
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_form'),
'access callback' => TRUE
);
return $items;
}
function my_module_form($form, &$form_state, $no_js_use = FALSE) {
$form['file'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#description' => t('Upload an image'),
);
$form['menu'] = array(
'#markup' => '<b>Add More:</b>'
);
$form['#tree'] = TRUE;
$form['names_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Add more images'),
'#prefix' => '<div id="names-fieldset-wrapper">',
'#suffix' => '</div>',
);
if (empty($form_state['num_names'])) {
$form_state['num_names'] = 1;
}
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['names_fieldset']['name'][$i][0]= array(
'#title' => t('Image'),
'#type' => 'file',
'#weight' => '5',
'#description' => t('Upload an image'),
);
}
$form['names_fieldset']['add_name'] = array(
'#type' => 'submit',
'#value' => t('Add one more'),
'#submit' => array('my_module_add_more_add_one'),
'#ajax' => array(
'callback' => 'my_module_add_more_callback',
'wrapper' => 'names-fieldset-wrapper',
),
);
if ($form_state['num_names'] > 1) {
$form['names_fieldset']['remove_name'] = array(
'#type' => 'submit',
'#value' => t('Remove one'),
'#submit' => array('my_module_add_more_remove_one'),
'#ajax' => array(
'callback' => 'my_module_add_more_callback',
'wrapper' => 'names-fieldset-wrapper',
),
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['#submit'][] = 'my_module_add_more_submit';
if ($no_js_use) {
if (!empty($form['names_fieldset']['remove_name']['#ajax'])) {
unset($form['names_fieldset']['remove_name']['#ajax']);
}
unset($form['names_fieldset']['add_name']['#ajax']);
}
return $form;
}
function my_module_add_more_callback($form, $form_state) {
return $form['names_fieldset'];
}
function my_module_add_more_add_one($form, &$form_state) {
$form_state['num_names']++;
$form_state['rebuild'] = TRUE;
//$form_state['no_redirect'] = TRUE;
}
function my_module_add_more_remove_one($form, &$form_state) {
if ($form_state['num_names'] > 1) {
$form_state['num_names']--;
}
$form_state['rebuild'] = TRUE;
}
function my_module_add_more_submit($form, &$form_state) {
$file = $form_state['values']['file']."<br \>";
$validators = array();
$file = file_save_upload('file', $validators, 'public://uploads');
print_r($file);
exit();
}
When i submit the form, i am trying to get the details of images, that are added through Add More option. But i am not able to get them. However i am only able to get the details of the first image (and able to upload it).
I want to know two things here:
How can i retrieve the details of the images that are added with Add More option (fieldset), and how can i upload them ?
When i browse and select an image in the fieldset, it is not retained in the form, after adding another image field. How to retain the selected images in fieldset ?
Look at this article -- http://evolvingweb.ca/story/poutine-maker-introduction-field-api-drupal-7-part-1 -- as it has some information on something missing in your code. $delta. $delta is the id assigned to values of fields, even if your field only has 1 item.
What do you see when you var_dump the file field you created? If you get all the information along with those added using 'Add one more' button, you can learn the correct structure of the values using: echo "<pre>"; print_R($form_state['values']); echo "</pre>":
There are a couple of issues with your code.
The first issue is that your submit function only deals with the first upload field which is indeed called "file". But does absolutely nothing to handle the other fields.
A second issue is that it will upload and save the first field every time you click on "Add one more" which will duplicate your upload. You would not experience this issue without AJAX, but if you want to add that, you will.
I would make the following changes:
Remove the $form['#tree'] = TRUE and add it to the fieldset instead. $form['names_fieldsets']['#tree'] = TRUE; after you declare the fieldset of course.
Change the way you declare the file fields in the fieldset (inside the for loop) to this:
for ($i = 0; $i < $form_state['num_names']; $i++) {
$form['names_fieldset'][$i]['name']= array(
'#title' => t('Image'),
'#type' => 'file',
'#weight' => '5',
'#description' => t('Upload an image'),
// We need this to know which file element this is.
// By default drupal would name all as files[names_fieldset]
'#name' => 'files[names_fieldset_' . $i . '_name]',
);
}
I would change the submit function like this (note that I assume you also do my above suggested changes):
function my_module_add_more_submit($form, &$form_state) {
if ($form_state['values']['op'] == 'Submit') {
$validators = array();
$files = array();
if (!empty($_FILES['files']['name']['file'])) {
$files[] = file_save_upload('file', $validators, file_default_scheme() . '://uploads');
}
foreach ($form_state['values']['names_fieldset'] as $name => $field) {
if ($name != 'add_name') {
$file_name = implode('_', $form['names_fieldset'][$name]['name']['#parents']);
if (!empty($_FILES['files']['name'][$file_name])) {
$files[] = file_save_upload($file_name, $validators, file_default_scheme() . '://uploads');
}
}
}
}
}
With this changes we set a form field name aware that's inside a tree. We only trigger uploads when the "Submit" button is clicked and only for form fields that actually had a file added to them. Also we upload using the default scheme and don't use always the public one.
Of course the code need some messages for the user to know how many files did upload, names, or any other deemed worthy info.

Validating an input in a Drupal form

I have a Drupal form wherein someone inputs information, and I need to do a database query to check if it is valid before submitting. I would like to either have a button the user can click to check the validity (or have it be done automatically after the user leaves that field), and then display some information about his selection.
I know I can use hook_form_submit to review a form when it is submitted and then stop the process if there are any errors, but I would like the user to be able to confirm they have selected the correct thing before submitting the form.
I haven't personally tried this module, but it might be what you're looking for:
http://drupal.org/project/ajax
If you're just looking for a way to do real-time lookups (e.g. entering the book barcode and getting the title), you can also use Drupal's autocomplete feature, but it will require you to write your own autocomplete function to handle the database lookups.
Take a look at: Basic form with validate handler. You really just need to add a function similar to mymodule_myform_validate($form, &$form_state) { ... }. From the linked page:
"This adds a new form field and a way to validate it with a validate function, also referred to as a validate handler."
<?php
function my_module_menu() {
$items = array();
$items['my_module/form'] = array(
'title' => t('My form'),
'page callback' => 'my_module_form',
'access arguments' => array('access content'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function my_module_form() {
return drupal_get_form('my_module_my_form');
}
function my_module_my_form($form_state) {
$form['name'] = array(
'#type' => 'fieldset',
'#title' => t('Name'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['name']['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE,
'#default_value' => "First name",
'#description' => "Please enter your first name.",
'#size' => 20,
'#maxlength' => 20,
);
$form['name']['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE,
);
// New form field added to permit entry of year of birth.
// The data entered into this field will be validated with
// the default validation function.
$form['year_of_birth'] = array(
'#type' => 'textfield',
'#title' => "Year of birth",
'#description' => 'Format is "YYYY"',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
// This adds a handler/function to validate the data entered into the
// "year of birth" field to make sure it's between the values of 1900
// and 2000. If not, it displays an error. The value report is // $form_state['values'] (see http://drupal.org/node/144132#form-state).
//
// Notice the name of the function. It is simply the name of the form
// followed by '_validate'. This is the default validation function.
function my_module_my_form_validate($form, &$form_state) {
$year_of_birth = $form_state['values']['year_of_birth'];
if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
}
}
?>

Categories