By zabej on
I've faced with an AJAX issue in Drupal 7.14
I have the next topic.
It is necessary to have a sum result (form i.e. 5 textfields) when somebody push Submit. It works. But when I change some data in the fields and push again the result remains the same as it was the first time.
I guess that AJAX doesn't reload array $form_state, when ajax_calculate_result is callbacked
Have you got any thoughts where the root of issue is?
Hope your help.
The actual code is below:
function some_module_calculate_form($form, &$form_state, $tid) {
foreach ($tid as $service_type) {
$form['reckon_'.$service_type->aid] = array(
'#type' => 'textfield',
'#title' => t('service measure pattern'),
'#size' => 5,
'#maxlength' => 5,
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Calculate'),
'#ajax' => array(
'callback' => 'ajax_calculate_result',
'wrapper' => 'calculate-rezult',
'effect' => 'fade',
'method' => 'replace',
)
);
$form['result'] = array(
'#markup' => 'Some result will be here',
'#prefix' => '<div id="calculate-rezult">',
'#suffix' => '</div>',
);
return $form;
}
function ajax_calculate_result($form, &$form_state) {
$sum = 0;
foreach ($form_state['values'] as $k=>$v) {
$sum += $v;
}
$form['result'] = array(
'#markup' => 'Total sum is ' . $sum,
);
return $form['result'];
}
Comments
Persisting values across Http requests
Hi,
As a web application, Drupal runs over Http. Http is a stateless protocal. That means values are not retained after a request/response pair is completed. You can create a "hidden" field(s) to retain your "sum" value between requests.
I would refer to the Drupal "Examples for Developers" multi-step form example:
http://api.drupal.org/api/examples/form_example%21form_example.module/group/form_example/7
Specifically "Tutorial 8"
http://api.drupal.org/api/examples/form_example%21form_example_tutorial.inc/function/form_example_tutorial_8/7
Hope that helps :)
Good Luck.
May be I wasn't clear in the
May be I wasn't clear in the explanation.
I need to have another sum because i change some values
in the first example i have
5 + 5 + 5
It types result 15
in the second example i change values in 1 and 3 fieilds
3 + 5 + 7
It types result 15 again =(
Please Elaborate
Hi,
Can you elaborate? What is the exact problem?
Both equations result in 15?? So, what is the issue?
Sorry, I got the impression that wanted to calculate a running total (e.g. 15+ 15 = 30, 30 + 5 = 35, 35+3 = 38).
What does this mean?
What does "types" mean???
Ops... pardon. I wrote not
Ops... pardon. I wrote not good examples... they have equal results.
1.
5 + 5 + 5 (output: 15)
2.
2 + 3 + 4 (output: 15, but it needs 9)
In the first and second example we have the same results
I found a decision. I use
I found a decision.
I use http://api.drupal.org/api/drupal/includes!ajax.inc/group/ajax/7
edit
to
$form['result'] = array(
'#markup' => 'Total sum is ' . $sum,
);
edit
to