2

As a follow-up to my last question regarding creating a dynamic select using JavaScript and the Forms API, I'm now wondering how do you actually get the options from that select list in a submit function. I've read "How to get All options of a 'select' field?" but I have yet to get it working with my code. Maybe because this person is using a CCK field and I'm using the Forms API which as far as I've read works differently. I've also tried pulling the data from the $form_state as well as the $form with no luck. Below is my code for a more complete picture of what I'm trying to do:

function _jstest_form($form, &$form_state) {
  $form['list'] = array(
    '#type' => 'select',
    '#options' => array('1', '2', '3'),
    '#size' => 4,
  );

  $form['text'] = array(
    '#type' => 'textfield',
  );

  $form['add_button'] = array (
    '#type' => 'button',
    '#value' => t("Add"),
    '#after_build' => array('_jstest_after_build'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
    '#submit' => array('_jstest_submit'),
    '#after_build' => array('_jstest_after_build'),
  );
  return $form;
}

function _jstest_submit($form, &$form_state) {
  //  Do stuff with options from select
}
3
  • In other words, you are trying to get the array contained in $form['list']['#options'], aren't you?
    – avpaderno
    Commented Dec 8, 2011 at 20:50
  • As you are referring to JavaScript (for the other question), are you trying to get that value from PHP, or JavaScript?
    – avpaderno
    Commented Dec 8, 2011 at 20:52
  • Yes that is essentially what I'm trying to do but $form['list']['#options'] doesn't actually work in submit. It gives me the original array (1,2,3) and not the ones that were added dynamically with JavaScript. And yes I'm trying to do this in PHP in the submit function of the module.
    – Sathariel
    Commented Dec 8, 2011 at 21:03

2 Answers 2

2

$form is the array build on server side, and it is not modified from JavaScript code executed from the browser, as that array is never passed to the browser. That is the reason why $form['list']['#options'] contains only the values set from the form builder.

The code that retrieves the value of $form is the following one, contained in drupal_build_form():

  // If the incoming input contains a form_build_id, we'll check the cache for a
  // copy of the form in question. If it's there, we don't have to rebuild the
  // form to proceed. In addition, if there is stored form_state data from a
  // previous step, we'll retrieve it so it can be passed on to the form
  // processing code.
  $check_cache = isset($form_state['input']['form_id']) && $form_state['input']['form_id'] == $form_id && !empty($form_state['input']['form_build_id']);
  if ($check_cache) {
    $form = form_get_cache($form_state['input']['form_build_id'], $form_state);
  }

When the form is submitted, that code retrieves the value of $form used to build the form.

0

I think if you want to pass the dynamic option list (client side javascript) back to the the server (PHP) then you've got two options:

  1. Have another, hidden, field that your javascript sets the value of at the same time that it is adding options to your select;
  2. Have your javascript execute a small AJAX callback that will get the server to save the values in the session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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