Hi,

I have multi step form registration and I am using profile2 form2 and i have created profile type Assess Health and added fields to it.
I have a custom module to add extra button to the form and i have added ajax callback button handler .

But i am not able to read the $form_State values in the callback handler.Here is my code :

function AssessHealth_form_alter(&$form, &$form_state, $form_id)
{
//drupal_Set_message('here i am');
if ($form_id == 'step_step_assess')
{

// This entire form element will be replaced with an updated value.
$form['html_div'] = array(
'#type' => 'markup',
'#weight' => '5',
'#prefix' => '',
'#suffix' => '',
);
$form['buttons']['another_button'] = array(
'#type' => 'submit',
'#value' => 'Assess',
'#weight' => '6',
'#ajax' => array(
'action' => 'click',
'callback' => 'Assess_callback',
'wrapper' => 'replace_div' ,
'name' => 'Assess',
),
'#executes_submit_callback' => FALSE,
);
}
}//end func

function Assess_callback($form, $form_state) {
$age = $form_state['values']['field_age'];

return $age;

}

Any help in this regard is really appreciated.

thanks,
Leena

Comments

Jaypan’s picture

But i am not able to read the $form_State values in the callback handler

What do you mean by this? How are you diagnosing it?

Also, a couple of other points:

1) Please wrap your code in <?php ?> tags. It's hard to read without it.
2) You should only use lower-case (small) letters in your code. Using upper-case (big/capital) letters opens up the potential for errors.

Leenass’s picture

Hi,

Thanks for your reply and sorry for the lack of readability in the coding .

I loop through the $form_State values and i check there is value but when i use this ,

$test = $form_state['values']['field_age'][LANGUAGE_NONE][0]['value'];

this is not returning any value but when loop through the form_State array i get the value.

Please help.

Jaypan’s picture

What do you see when you do:

<?php
drupal_set_message
('<pre>' . print_r($form_state['values'], TRUE) . '</pre>');
?>
Leenass’s picture

thanks alot ,it really helped.I checked it using
dpm('

' . print_r($form_state['values'], TRUE) . '

');

I am using profile2 form so it had one more array and the correct syntax :

$test = $form_state['values']['profile_main']['field_goal']['und'][0]['value'];
I was missing profile_main in the above statement.

I am slowly understanding the form API and thanks for your valuable support and time.
I really appreciate.

Thanks,
Leena

Ayesh’s picture

And just a tip - you don't have to use print_r when using dpm. You can throw whatever you want to inspect, and it can build a nice interactive tree of the variable.
dpm($form_state);

// Ayesh
(Latest blog post: Drupal bad practices)