I have a dropdown select box and a textarea. I want to update the textarea each time I change the value of the dropdown select box. The value of the textarea will come from the database and will depend on the value of the dropdown select. I was able to update the textarea each time the select box is changed. However, when I add something to the value in the textarea or change it to something, I still get the same value when it was changed onselect when I submit the form:

<?php

function mymodule_form_builder($form, $form_state) {
  $form = array();
  $form['term'] = array(
    '#title' => t("Choose a term"),
    '#type' => 'select',
    '#options' => array(
        'one' => 'Option 1',
        'two' => 'Option 2',
        'three' => 'Option 3'
    ),
    '#ajax' => array(
      'callback' => 'mycallback',
      'wrapper' => 'textarea-div',
    ),
  );
  
  $form['mytextarea'] = array(
    '#title' => t('Keywords For This Term'),
    '#type' => 'textarea',
    '#prefix' => '<div id="textarea-div">',
    '#suffix' => '</div>'
  );
  
  if (!empty($form_state['values']['term'])) { 
    $form['mytextarea']['#value'] = datafromdatabase($form_state['values']['term']);
  }
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'), 
  );   
  
  return $form;  
}

function mycallback($form, $form_state) { 
  return $form['mytextarea'];
}

?>

Each time I change the "term" dropdown select, the "mytextarea" textarea, get's filled with the value from the database. However, when I add or change that value and press Save, the value of $form['mytextarea'] is still the same value after I changed the dropdown select but before I updated or changed it.

Please help!

Comments

jason_gates’s picture

Hi.
If I understand your question correctly, it really doesn't have anything to do with select lists or ajax. Your question deals with what happens when a user clicks the "submit" button. In your example, clicking the submit button is not an ajax call/event, it's a standard synchronous post (aka submit). Therefore, please post your submit handler.

You reference a method called "datafromdatabase()". Do you have a corresponding method that persists (i.e. adds/updates) data? Does your submit handler call your persistence routine?

Please elaborate.

Mark Vincent Verallo’s picture

My submit handler simpley prints_r the $form_state['values']. Right now I'm just trying to check if what I enter into the textarea will be passed when I submit the form. Unfortunately, it's not. Also about the "datafromdatabase()", yes, I have that method and it simply returns a string that will be filled in the textarea. Try to see the post below, he seems to understand my problem.

graysadler’s picture

if (!empty($form_state['values']['term'])) { 
  $form['mytextarea']['#value'] = datafromdatabase($form_state['values']['term']);
}

You are setting the value of that field here. No matter what you enter into the text area after the form is rendered, the value will always return to the value set here.

Maybe you meant to do this?

if (!empty($form_state['values']['term'])) { 
  $form['mytextarea']['#default_value'] = datafromdatabase($form_state['values']['term']);
}

Lead Developer and Founder of StreamRiot.com

Mark Vincent Verallo’s picture

Hi gsadler, I'm glad you understood my question. You are right that no matter what I enter into the form field it will not be submitted. I also have tried setting the default_value but it's not working. I guess you cannot set the default_value after the $form_state['values'] is set because setting the default value will work the first time I load the form, however, after I change the dropdown select (triggering the Ajax call behind, thus, submitting and setting $form_state['values']), I can't be able to set the default_value.

Mark Vincent Verallo’s picture

In the Drupal API reference it says that we should not modify the $form or the $form_state in the callback function. But I tried, to do this and it solved my problem! The callback function now looks like the following:

function mycallback($form, $form_state) { 
  $form['mytextarea']['#value'] = datafromdatabase($form_state['values']['term']);
  return $form['mytextarea'];
}

Thanks for those who tried to help!

SilviaT’s picture

I have the same problem here: this only happens with the textarea field for me.
Anybody knows if this is a bug?

jaypan’s picture

Try unsetting $form_state['input'][ELEMENT_NAME] in your form definition. For example, if your form element looks like this:

$form['my_element'] = array
(
  '#type' => 'textfield',
  // remainder of form definition here
);

You would put this before/after it:

  unset($form_state['input']['my_element']);

Contact me to contract me for D7 -> D10/11 migrations.

SilviaT’s picture

Thanks Jay, it works perfectly!

SilviaT’s picture

[double post]