Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By tomas.teicher on
Hi I have form element as follows:
<?php
$form['name'] = array(
'#type' => 'textfield',
'#title' => $title,
'#default_value' => $name,
'#size' => 40,
);
drupal_set_message($form['name']['#default_value' ]);
?>
I change $name and $title variable with ajax callback. Title changes correctly. drupal_set_message also returns changed default value correctly but I still can see the original value in the form.
Can anybody help?
Comments
Cant you use
Cant you use jQuery("id).val(); to change it in the font, you are already using Javascript right?
You said everything did change correctly within the PHP, so it should just be a output problem.
you don't need #default_value in drupal_set_message
you don't need #default_value in drupal_set_message, just ['name'] is enough.
Without seeing the whole
Without seeing the whole function, its hard to tell where $name comes from.
Default value only used when form is first rendered?
My guess is that the default value is not displayed on the form so you can't change it with AJAX. The default value is just the initial value that is put into $form['name']['value'] when the form is rendered. You can probably change the default value but without re-loading the form you won't see it?
Not quite
I set a #default_value on my text field, then check for form_state and set it again, but when the ajax reloads the form, the default value is set to null, even though it is correct if I do a dpm in my callback. Any more clues?
The other Andrew Morton
Fixed
I have been struggling with this issue too, although have just found out why this is happening...
Even if the #default_value is set correctly, and $form['element_name']['#default_value'] technically contains the right information, the value held in #default_value will only be displayed if $form_state['input']['element_name'] is EMPTY. If there is a value in $form_state['input']['element_name'], whatever it is, then $form['element_name']['#default_value'] will not be shown. By definition, the #default_value is only shown if there is no previously entered information to display!
In my situation I have a textfield, and I want to control the text inside it using a set of radio buttons with an ajax callback. When a different button is selected, the text in the textfield will change. I set this text using the $form['textfield']['#default_value'] element, but to get the default value to show I have to say unset($form_state['input']['textfield']) before the form element is defined.
A liitle more help?
I have a similar problem with select lists . What do you mean "before the form element is defined"?
I tried, following your guide lines , inserting the unset($form_state['input']['textfield']) inside the ajax callback function but it did not worked.
Thank you Thank you THANK YOU!
I've been beating my head against the wall for 2 days. Unsetting the $form_state['input']['element_name'] does the trick!
Thanks for this tip! Pulling
Thanks for this tip! Pulling my hair out all morning on this issue
Thank you melissavdh I had
Thank you melissavdh I had been trying to figure this one out from the last hour .. :)
Had read it before in the documentation but forgot while implementing. I have been trying to alter a webform, and have noted that the fields that i had created in the form_alter function did not have this restriction but the fields that were created by the webform module did... I wonder why?
Regards,
Sumaiya Javed
Web Developer
www.sumaiyajaved.com
www.phpjavascript.com
Is this right way?
Unsetting $form_state['input']['element_name'] will work but what if form is rebuilding after failing in validations ? I think, It will again display your '#default_value', which seems wrong. It should display entered value. Any thoughts ??
-- tanmayk
I had the same problem,
I had the same problem, default value does not change on ajax load but it works when I replace #default_value with #value.
Field not-editable with default value but unset doesn't work
Hi all,
I looked at all your posts. In some special cases (i.e. when I receive specific url parameters), I want a checkbox field of a form to be no-editable with a default value. When I hit the form, the checkbox is greyed out and is checked (which is the default value I set). But the field value when the form is submitted is not the value I expect, as the checkbox is not checked.
I guess I only change the form when displayed, how can I also change the value of the checkbox ? Thank you.
Here is my code:
Yes I got it finally
All right, as I mentioned, the default value is only used for display. One would have to re-populate the value when the form is submitted. Here is the code:
About AJAX callbacks and #default_value in form elements
I found this thread after running into a similar problem when trying to use #default_value in form elements when using AJAX callbacks. Here is some example code for doing this:
Thank you.
Developer shouldon't set '#default_value' for AJAX changes. I've got this problem.
'#default_value' SHOULD BE
'#default_value' =>key($options) WHERE '#options' =>$options.
related issue
https://www.drupal.org/forum/support/module-development-and-code-questio...
Don't use #default_value in ajax callback
Simplest solution is to use #value in ajax callback.
#default_value won't work
You saved my life
You saved my life
Rajveer Singh
rajveer.gang@gmail.com