Hello,

im giving up now on trying-to-solve-everything-by-myslef...

last (many) hours ive been trying to get select list selected values when submitting form.
google have been a good friend but seems that developers for drupal are not giving out information so easily.

anyhow, ill try here.. as a last shoot to hit the target.

im creating a module, which adds custom fields to node editing/creation page.
i also add a select list, which works fine, but when updating/adding this old/new node, i get nothing out from this freakin select list.

heres what i got:

function flashmodul_form_alter(&$form, $form_state, $form_id) {
	global $base_url;

	//*** here is some irrelevant code for this problem

	if ($form['#id'] == 'node-form' && $node_types[$form_type] && user_access('use flashmodul')) {
		$form['flashmodul_node'] = array(
			'#type' => 'fieldset',
			'#title' => t('FlashModul'),
			'#description' => t('Select SWF file to publish in this node.'),
			'#collapsible' => true,
			'#collapsed' => false
		);
		$form['flashmodul_node'] = array(
			'#type' => 'select',
			'#title' => t('Listing SWF files in folder "<em>root/'.$swfMediaFolder.'</em>"'),
			'#default_value' => variable_get('flashmodul_node_swf',''),
			'#value' => variable_get('flashmodul_node_swf',''),
			'#options' => $fileArr,
			'#multiple' => true
 		);
 		$form['#submit'][] = 'flashmodul_node_submit';
 	}

	return $form;
}

all good here... works fine. but callback function when submitting form doesnt do any good. notice the last rows are as they are, ive been trying for many hours but none of the attempts worked. (google didnt help at all)

function flashmodul_node_submit($form, &$form_state){

    $choosenFlash = $form_state['values']['flashmodul_node'];
    $nid = $form_state['values']['nid'];
    $existtingID = db_query("SELECT * FROM {flashmodul} WHERE nid='%d'",$nid);
	while ( $obj = db_fetch_object ($existtingID) ) {
	  $db_result = $obj->fid;
	}
	if(!empty($db_result)){
		$res = db_query("UPDATE {flashmodul} SET fid='4', flashfile='6' WHERE nid='".$nid."' AND fid='".$db_result."'");
		drupal_set_message(t('Flash uppdaterad!'));
	}else{
		$res = db_query("INSERT INTO {flashmodul} (fid,flashfile,nid) VALUES (%d,%s,%d')",db_next_id('fid'),'',$nid);
		drupal_set_message(t('Flash sparad!'));
	}
	
	// one of the many shots for trying to nail this problem
	
	//$form_state['values']['swf_files'] = array('0' => array('value' => $form['swf_files']['value']));
	$sel = $form_state['values']['swf_files'];//$form_state['values']['swf_files'][0];// $form_state['values']['swf_files'][0]['value'][0]['#value'];
	drupal_set_message($sel);
/*	foreach($sel as $it){
		drupal_set_message($it->value);
	}*/
}

so if there is anyone kind enough to post a straight-forward solution for my problem, ill by you beer when you come to visit me :)

Comments

Jaypan’s picture

I see two problems. The first is that you are declaring field set, then immediately overwriting it with your select list. Why even declare the field set? That's not where your issue lies though. The problem is that you've declared a #value in your form definition for the select list. #value cannot be overwritten no matter what the user selects. You want to only use #default_value.

kscheirer’s picture

You shouldn't be using both #default_value and #value, those are incompatible with each other! Stick to just #default_value.

Also, you're doing this twice...

  form['flashmodul_node'] = array( // fieldset definition ... 
  );
  form['flashmodul_node'] = array( // select list definition overwrites the fieldset ...
  );

try something more like this...

  form['flashmodul_node'] = array( // fieldset );
  form['flashmodul_node']['swf_files'] = array( // now define the select list inside the fieldset );

then the selected value will be available in the submit hook as something like this $form_state['values']['swf_files']. If you want to see whats in the $form_state['values'] array during the submit hook, print it out. If you have the devel module installed (recommended), use dsm($form_state['values']); or with just stock drupal drupal_set_message('<pre>' . print_r($form_state['values'], 1) . '</pre>');.

Hope that helps!
-ks

danel’s picture

thank you so much!
your suggestion to ['flashmodul_node']['swf_files'] is actually what i originally had.
but it didnt work, i got nothing in output

yes i got devel installed till now you posted a amazing way to trace out data ! dsm() is my new buddy :D

what i see in dsm is swf_files but this array is empty, even tho i selected one item i the list
so conclusion is that ive always done it all right (though drupal messed with me) but no values are passed :(

i also use Charles web-proxy to track data sent over http and there i see that when i save my node
something is passed, is shows: swf_files[] 1
(its the value from this item)

but in dsm() i get this (i also tested to select 2 items but output is same)
swf_files (String, 0 characters )

danel’s picture

SOLVED:

found tha evil-doer in my code
problem was values, after commenting these out, i got value for my item when submiting form

		$form['flashmodul_node']['swf_files'] = array(
			'#type' => 'select',
			'#title' => t('Listing SWF files in folder "<em>root/'.$swfMediaFolder.'</em>"'),
			//'#default_value' => variable_get('flashmodul_node_swf',''),
			//'#value' => variable_get('flashmodul_node_swf'),
			'#options' => $fileArr,
			'#multiple' => true
 		);
Jaypan’s picture

#default_value is fine. #value is the one you need to remove. #default_value can be overwritten, #value cannot.

danel’s picture

cool jay! thnx for you help :)

much appriciated!

/danel