Forums

CF v6 - Change value if field not empty

lifecolor 07 Oct, 2019
hi, sorry but I'm using chronoforms v6 for the first time.
I have 3 fields: fieldone, fieldtwo, fieldthree.
I would like to visualize in the following custom html:
the value "0" if all three fields are empty
the value "10" if only one is not empty
the value "20" if two are not empty
the value "30" if all three are not empty
how can I do?
Life Color Communication srl
GreyHead 07 Oct, 2019
Hi lifecolor,

You can write a small JavaScript function to set the value of a readonly text input. It would check the three inputs and set the value accordingly. You can then go to each of these three inputs open the settings, open the Events tab. Then set the 'triggering event' (the first box to Change, and the 'Triggered action' to the name of your JavaScript function.

Bob
lifecolor 07 Oct, 2019
Bob sorry but ...
in all field event
In Triggering event: Change
Triggered action (s): Function
Affected elements CSS selectors: namefunction

In content javascript ??

function namefunction () {
var value = 0;
if (fieldone! = "") {value = value + 10;}
if (fieldtwo! = "") {value = value + 10;}
if (fieldthree! = "") {value = value + 10;}
alert (value);
}

How do I call the "fieldone" field?
How can I write the value instead of the alert?
Life Color Communication srl
healyhatman 07 Oct, 2019

Affected elements CSS selectors: namefunction


namefunction is not a CSS selector, so it doesn't go in the CSS selector box. It's a string value name of a function, so it goes in the value box.
lifecolor 07 Oct, 2019
But I don't see the value box..

in this way when I start writing this field I call javascript.
but I don't know how to write the javascript that reads my data and writes the value
Life Color Communication srl
healyhatman 07 Oct, 2019
It's the box on the left that says possible value
lifecolor 08 Oct, 2019
ok..
but if i write namefunction on the left when i write in this field the alert does not work 😢
Life Color Communication srl
healyhatman 08 Oct, 2019
To get the values you use
jQuery('input[name="fieldnamegoeshere"]').val()
lifecolor 08 Oct, 2019
perfect..

function namefunction(){
var fieldone = jQuery('input[name="email_aggiuntiva"]').val();
var fieldtwo = jQuery('input[name="email_aggiuntiva2"]').val();
var fieldthree = jQuery('input[name="email_aggiuntiva3"]').val();
var fieldfour = jQuery('input[name="email_aggiuntiva4"]').val();
var fieldfive = jQuery('input[name="email_aggiuntiva5"]').val();
var valore = 0;
if (fieldone != ""){ valore = valore + 10; }
if (fieldtwo != ""){ valore = valore + 10; }
if (fieldthree != ""){ valore = valore + 10; }
if (fieldfour != ""){ valore = valore + 10; }
if (fieldfive != ""){ valore = valore + 10; }
alert(valore);
}

but.. now i would change the alert and write the value "valore" in a text html.
can you help me? 🙂
Life Color Communication srl
healyhatman 08 Oct, 2019
Answer
jQuery('#idOfTheHTMLBlock').html('The value is now ' + valore);
lifecolor 08 Oct, 2019
thank you so much !!!!!!!! 🤣🤣🤣
Life Color Communication srl
lifecolor 05 Nov, 2019
sorry .. last thing.
how do I print this value in the body of the email?

You can use HTML, text and chrono commands, e.g: {data:field_name}
Life Color Communication srl
healyhatman 05 Nov, 2019
Recalculate it again on submit using PHP. Return it from the php function and include it in your email using {var:php_block_name}
lifecolor 06 Nov, 2019
Sorry, but I did not understand😟
I have insert in submit - action a block php.

Name: php4
Code:
$mail1 = "{data:email_aggiuntiva}";
$mail2 = "{data:email_aggiuntiva2}";
$mail3 = "{data:email_aggiuntiva3}";
$mail4 = "{data:email_aggiuntiva4}";
$mail5 = "{data:email_aggiuntiva5}";
$valore = 390;
if ($mail1 != null) {$valore=$valore+250;}
if ($mail2 != null) {$valore=$valore+250;}
if ($mail3 != null) {$valore=$valore+250;}
if ($mail4 != null) {$valore=$valore+250;}
if ($mail5 != null) {$valore=$valore+250;}
echo $valore;

but in this way print a wrong value.
if I have an empty email the "if condition" nothing is not calculated well .. the final value is always 1640

and then...
instead of printing the value, how can I insert it in the body of the email?

{var: php4}
I see nothing

thanks
Life Color Communication srl
healyhatman 06 Nov, 2019
Can';t use shortcodes in PHP. Use $this->data('email_aggiuntiva')
lifecolor 07 Nov, 2019
perfect... thanks
Life Color Communication srl
This topic is locked and no more replies can be posted.