data(field_select19,,); // Put the value of the dropdown in $s$s=$s*2; // multiply that value with 2$this->data(field_select21,$s,true); //Put that value in textfield.Under the php code box a custom code box within {view:field_text21}Can you tell me what i am doing wrong ?"> How to get the result of a calculation in a php code box in a textfield - Forums

Forums

How to get the result of a calculation in a php code box in a textfield

JoCo 25 Sep, 2019
This is the situation:
A dropdown field (field_select19) within values from 1 to 10
In that dropdown under the event-tab the settings : Change, Reload, in the affected element: the name of the textfield
The above textfield (field_select21) with the settings under the General tab value {var:php59} and under the Advance tab the reload event name "calculation".
An event named "calculation" within a php code box named php59. In that box a simple calculation:
$s=$this->data(field_select19,,); // Put the value of the dropdown in $s
$s=$s*2; // multiply that value with 2
$this->data(field_select21,$s,true); //Put that value in textfield.
Under the php code box a custom code box within {view:field_text21}
Can you tell me what i am doing wrong ?
healyhatman 26 Sep, 2019
return the value. So
return $s * 2;
JoCo 26 Sep, 2019
Thank you healyhatman for you quick respons but i did not get any further.
I this moment i am using this code in the php code box:
$s=$this->data("field_select19","',);
$s=$s * 2;
return $s;
And then when i do a preview the field_text21 shows only a grey turning cicle.

I've tried the simplest code return "Hello". In the debugger is saw as returned and as var but the text did not appear in the textfield.
Please help me again. Maybe with a working example. It is so frustrating to always see that grey turning cycle.
healyhatman 27 Sep, 2019
Try opening the reload event directly in your browser. If you don't know what it is, right click on the field that gets reloaded and inspect it, and look for the "data-reloadurl" property. IF you go there and there are any errors, you should be able to see them.
JoCo 27 Sep, 2019
Thank you again.
This is what i did. I put the cursor on the element with the spinning circle and choose the option "Inspect element".
I see the following error : Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Then i change the content of the php box with a simple valid statement "return "Hello". No error after inspecting the element. But I still saw no text in that textfield.
Then I disabled that textfield and "eureka". The text is then visible. So one little problem solved.
But still the error with the calculation stays. Maybe the statements in the php box are not valid?
healyhatman 27 Sep, 2019
So you skipped a step though - you should actually go to the data reloadurl. Type it in to your address bar.
JoCo 27 Sep, 2019
That is the problem. When i inspect the element during the spinning circle i do not get any info about a data reload url.
Thats why i did a test with a valid statement to see where the data reload url appears.
The only error i see i what i mentioned above. Are there other possibilities?
JoCo 27 Sep, 2019
My response above is already obsolete.
I have tried this : return $this->data("field_select19","1");
Yes, again a small succes. Now i see a respons in that concerning textfield. I put everyting between double quotes.
The respons is the value 1. I hoped to see the value of the triggering dropdown (=field_select19). What must i do with "default value" in $this->data("field_name","default value")?
Is it valid to put a value in a variable like $s=$this->data("field_select19","1")
JoCo 27 Sep, 2019
Answer
1 Likes
Also my response above is obsolete again.
It is working. I did a multiplication and the result was correct. The 2 reasons why it failed every time where the missing quotes and the use of the wrong fieldname. I now used the name under the General tab.
So healyhatman thank you for spending your time with me. Till next time.
JoCo 01 Oct, 2019
Hello,

my next challenge.
In my form (results of tennismatches) a have several textfields where to i write a value. That value is calculated in an event/php box depending on a value in two dropdown fields (results of a tennis game ).
Because i want to prevent the user to change those textfields i disabled them. I found out that i must disabled those textfields else i could not write the value in it.
Those text fields appears in the form with a grey labelname.
But the result of this situation is that when i send that form i see a list of all the disabled fieldnames in a red box above that form. I also noticed that nothing is saved to the database.
So what i want is:
- Write a value to a textfield (or another field?)
- That textfield must be disabled/blocked for change by the user.
- That textfield must be available for calculation in a php box
- That textfield should be saved in a database.
Does anyone have a suggestion ?
healyhatman 01 Oct, 2019
OK a few things.

Disabled fields aren't sent with form data. You want 'readonly'.
Just because it's disabled / readonly, DOES NOT MEAN A USER CAN'T CHANGE IT! You should calculate it to display to the user, and then CALCULATE IT AGAIN in the backend to save it to the database.
JoCo 01 Oct, 2019
Thank you healyhatman for your quick respons.
In what way or field should i display it to the user? I cannot write a value in an enabled textfield.
healyhatman 01 Oct, 2019
Just display it in a HTML box, or in a text box, or however you want it doesn't matter. Like I said when you submit the form, you should completely ignore the field and instead recalculate the value before storing the recalculated value in the database.
JoCo 01 Oct, 2019
Oke, thank you. I must find another way. It concerns 16 calculations.
How does the form deal with hidden fields?
healyhatman 01 Oct, 2019
You mustn't "find a way". I've told you the way.

If you send the calculations as a hidden field, the user can change them. If they're read-only the user can change them. You need to just recalculate the values after submission.
JoCo 01 Oct, 2019
Thank you healyhatman,
I will give it a trie.
JoCo 02 Oct, 2019
Hello,

in my form there are 4 field_text fields in a multi field area.
When i write something in the first field and i jump to the next text field the first field get a rose/red color and underneath appears the fieldname also in red.
That goes also for the other text fields
Something tells me this is not good. What causes that color and is it an error indication? And especially how do i get rid of it.
JoCo 02 Oct, 2019
Hello,
I already found a way to remove the red color in the question above. If this is the solution i don't know.
I unchecked the option "Email" under the Validation-tab for the text fields

But now i encounter a new problem. I trie to send an email with the form in it. What i see after SEND is an email body with only two fields in it and then an array with all the field of the form filled with values and then another array with the email body.
Why is the rest not in it ?

Its very frustrating that nothing goes by itself. ChronoformsV5 was in that way much easier.



1e HD ontvangendBerry de Jager
2e HD bezoekendBen Swillens

Array
(
    [option] => com_chronoforms6
    [cont] => manager
    [chronoform] => wvzuitslagenv2
    [event] => submit
    [tvout] => view
    [load-form] => Array
        (
            [counter] => 0
        )

    [_counter] => 0
    [datum] => 2019-10-01 18:46:00
    [weekdag] => zat
    [poule] => 1
    [klasse] => 1
    [hal] => sus
    [ontvteam] => 3
    [bezteam] => 1
    [he1ontv] => 22480196
    [he1bez] => 18380778
    [gamesvoor1he] => 0
    [gamestegen1he] => 0
    [puntenvoor1he] => 0
    [puntentegen1he] => 0
    [he2ontv] => 22480196
    [he2bez] => 18380778
    [gamesvoor2he] => 0
    [gamestegen2he] => 0
    [puntenvoor2he] => 0
    [puntentegen2he] => 0
    [he3ontv] => 22480196
    [he3bez] => 18380778
    [gamesvoor3he] => 0
    [gamestegen3he] => 0
    [puntenvoor3he] => 0
    [puntentegen3he] => 0
    [he4ontv] => 22480196
    [he4bez] => 18380778
    [gamesvoor4he] => 0
    [gamestegen4he] => 0
    [puntenvoor4he] => 0
    [puntentegen4he] => 0
    [hd1ontv1] => 22480196
    [hd1ontv2] => 22480196
    [hd1bez1] => 18380778
    [hd1bez2] => 18380778
    [gamesvoor1hd] => 0
    [gamestegen1hd] => 0
    [puntenvoor1hd] => 0
    [puntentegen1hd] => 0
    [hd2ontv1] => 22480196
    [hd2ontv2] => 22480196
    [hd2bez1] => 18380778
    [hd2bez2] => 18380778
    [gamesvoor2hd] => 0
    [gamestegen2hd] => 0
    [puntenvoor2hd] => 0
    [puntentegen2hd] => 0
    [totaalgamesvoor] => 6
    [totaalgamestegen] => 8
    [totaalpuntenvoor] => 8
    [totaalpuntentegen] => 8
    [opmerking] => 
)

Array
(
    [email120] => Array
        (
            [recipients] => Array
                (
                    [0] => joscoenen@kpnmail.nl
                )

            [subject] => New ChronoForm email received.
            [body] => <table width="100%" cellpadding="5" cellspacing="3" border="0" class="ui table"><tr><td width="30%" valign="top" align="right"><strong>1e HD ontvangend</strong></td><td width="70%" valign="top" align="left">Berry de Jager</td></tr><tr><td width="30%" valign="top" align="right"><strong>2e HD bezoekend</strong></td><td width="70%" valign="top" align="left">Ben 	Swillens</td></tr></table>
            [result] => the Mail sent successfully.
            [var] => 1
        )

)
healyhatman 02 Oct, 2019
Why did you have email ticked in fields that weren't for email addresses? Do you have individually named fields, or a repeater area since it looks like you have copies of the same field with bigger numbers? What does your email action look like?
JoCo 03 Oct, 2019
Hello healyhatman,
what do you mean by that "email ticked fields"?
All the field are dropsdowns filled with a name/number pair. The last 5 fields are 4 textfield and a textaera field. The number in all the fields is a member number.
Because the most field are filled with the same name that why it looks like copies. But that isn't.
My email action is the email from Basic under "submit-->action". The "Enabled " is checked. The "Recipients list" is filled with my email address.
"Subject" is filled.
"Auto content" = Include email enabled fields " Send as HTML".
"Auto Attach file fields" is checked
Is this info enough?
JoCo 04 Oct, 2019
Hello,

i have already solved the problem i describe in JOCo 38. So don't bother anymore for that problem. Thank you.
I still have another question. Is it possible to write a value that i calculate in an event with with a php box to a hidden field and how i activate that event?
GreyHead 04 Oct, 2019
Hi JoCo,

I'm not completely sure what you need here - but you should be able to create a value and add it to the form data using the name of the hidden input - for example
<?php $this->data('hidden_name') = 999; ?>

Then it will be set as the value of the hidden input.

NB if this is sensitive data then don't use hidden inputs to store it - add it to the user session instead. Form data, including hidden data, can be edited by the user.

Bob
This topic is locked and no more replies can be posted.