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 AndreyUA on
Hello. I'm trying to make a quite simple task in my module - load values from DB using one value. Saying simple: there is a form with a bunch of fields. When I fill email field I need to load it's settings and fill the form with them. That simple.
But I can not make my button change value in a textfield.
What I have:
function mymodule_form( $form, &$from_state){
if(isset($form_state['values']['your_name']))
{
$name_value = $form_state['values']['your_name'];
}else{
$name_value = "";
}
$form = array();
$form['your_name'] = array(
'#title' => t('Your or your company name'),
'#type' => 'textfield',
'#value' => $name_value,
'#prefix' => '<div id ="subscribers-name">',
'#suffix' => '</div>',
);
$form['load_settings'] = array(
'#type' => 'button',
'#value' => t('Load my settings'),
'#submit' => 'simplenews_mod_load_subscriptions',
'#ajax' => array(
'wrapper' => 'subscribers-name',
'callback' => 'simplenews_mod_load_subscriptions_ajax',
'method' => 'replace',
)
);
return $form;
}
function simplenews_mod_load_subscriptions( &$form, &$form_state)
{
$form_state['values']['your_name'] = "loaded value";
$form_state['rebuild'] = TRUE;
}
function simplenews_mod_load_subscriptions_ajax( $form, &$form_state)
{
return $form['your_name'];
}
With this code I expect that the only text field in my form will be filled with loaded value text when I press the Load my settings button that's all. But it does not. What am I doing wrong? Please suggest. I'm struggling with this for over 3 days.
Thank you
Comments
1)
1)
2) Change $form_state['values']['your_name'] to $form_state['storage']['your_name'] in all locations.
array in the callback was the key
Thank you Jaypan. As always the less visible item is the most visible one.
Double post