1

On this page I have an select list with values from a file directory. This works. Only after submitting it doesn't get the value of it. It makes it "1". Normally with a "normal" select list this should work I guess.

Problem: $form_state['input']['edit']['ios_cert'] doesn't return selected value.

Code (in hook_form):

$files = array();
foreach(glob("C:/wamp/www/drupal2/sites/default/files/*.pem") as $filename) {
   array_push($files, substr(strrchr($filename, '/'), 1));
 }

$form['replace_textfield']['edit']['ios_cert'] = array(
    '#title' => t('Select right certificate file'),
    '#type' => 'select',
    '#options' => $files,
    '#default_value' => $ios_key,
    '#weight' => 45,
  );

Code (hook_submit):

$ios = $form_state['input']['edit']['ios_cert'];

$query = db_update('push_notifications_api')
->fields(array(
'app_id' => $app_id,
'GCM' => $gcm,
'ios' => $ios,
'ios_mode' => $ios_mode,
))
->condition('app_id', $oldid)
->execute();
drupal_set_message(t('@title has been updated succesfully.', array('@title' => $title))); 
dsm($form_state);

Result:

dsm result

And it should be something like "Filename-djfklasjklf.pem"

thanks in advance!

2 Answers 2

1

Currently you're setting the '#options' property of the form element as an indexed array such as:

$form['replace_textfield']['edit']['ios_cert']['#options'] = array(
  0 => 'filename1.pem',
  1 => 'filename2.pem',
  [...]
);

This means that the first option in the select tag will have a textual label of "filename1.pem" and a value of 0, so 0 will be sent via GET/POST to the receiving script.

In order to send the filename you need to also have the filename as the key of each option in the '#options' array (i.e it should be a self-keyed associative array).

A simple way to achieve that would be to change the line:

'#options' => $files,

to:

'#options' => array_combine($files, $files);

or, change:

array_push($files, substr(strrchr($filename, '/'), 1));

to:

$filename = substr(strrchr($filename, '/'), 1);
$files[filename] = filename;
2

I already got it.

I added this:

foreach ($files as $record){
    $options1[$record] = $record;
 }

And then use $options1 as '#options'

1
  • Yup, #options is an associative array of key:label. The array value is only used in the user-facing text, and might be a translated string, while the array key is what actually gets submitted. Apr 24, 2015 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.