## Inner field calculation

translate

Re: Is there a way to calculate with form fields?

Posted by Oliver Georgi on Tue Dec 16, 2008 18:30
A simple approach how to get this going is making use of (just an example):

a) 1 x hidden field (hidden_field_name) within your form → this will be the target for your calculation and will be taken over into your mail form then. b) additionally a frontend_init script “my_form_init.php”:

``` if(isset(\$_POST['fieldname'])) {
// hier die Berechnung durchführen - an der Stelle ganz beispielhaft realisiert.
\$_POST['hidden_field_name'] = xss_clean(\$_POST['fieldname']) * 1000;
}```

## Example 01

I'm setting up a site for a customer who will add a lot of articles describing different products. I don't want him(my customer) to set up a form for every product.
I want to “call” a form from the template with some sort of identification of the current article.

This is described also a thousand times inside the forum - working like “send a friend”.
Use an article with content part based on your needs outside public access.
Then use {SHOW_CONTENT:CP,ID} (ID = ID of the content part form) inside of your template.
It's no “out of the box” solution - but as I said it is discussed somewhere here in the forum.

### Finish

translate

Create the file /template/inc_script/frontend_init/check.php
```<?php
if(isset(\$_POST['eingabe'])) {
// do your calculation here - this is an example.
\$_POST['versteckt'] = xss_clean(\$_POST['eingabe']) * 1000;
}
?>```
Now you can do your calculations.
In our example the field input is multiplied by 1000, while the output is showing the input value and the result.

Insert a default for the hidden field (hidden). Define this field as “required”.
(If an error is encountered, set the field value to ”” (\$_POST['my_hidden_field'] = ””;) ).

Result: Error → if the value is validating, then the hidden field value will too contain the valid result.
english/phpwcms-system/article/contentparts/form/special-applications/inner-field-calculation.txt · Last modified: 2009/05/22 13:26 by Claus