Buy Now
Sign in

How to show result of calculating js in message?

WoodyF4u , April 24 2015
WoodyF4u

Hi,
I am not a js programmer, but I found a script example for counting fields of my form.
In the form it is possible to add the names of max 6 people.
All of these people can select an other membership. So the prise is different.
At the end of the form the visitor will press the submit button.
At the "thank you" message I wil show the total amount of the 6 fields.
I have placed this script in the on load event:

				window.addEvent('domready', function() {
function startCalc(){
//interval = setInterval("calc()",1);
   calc();
}
function calc(){
   if (document.getElementById){
      een = document.getElementById('abo_keuze01').value;
      twee = document.getElementById('abo_keuze02').value;
      drie = document.getElementById('abo_keuze03').value;
      vier = document.getElementById('abo_keuze04').value;
      vijf = document.getElementById('abo_keuze05').value;
      zes = document.getElementById('abo_keuze06').value;
      document.getElementById('bedrag').value = (een * 1) + (twee * 1) + (drie * 1) + (vier * 1) + (vijf * 1) + (zes * 1);
   }
}
function stopCalc(){
clearInterval(interval);
}
});

At the end of the script, the amount is ready as 'bedrag'.
But when I show {bedrag} in the message text, only {bedrag} is shown in the text.

What can I do to show the amount the vistor have to pay in the message text?

Best regards,
Wouter
The Netherlands

WoodyF4u

Thanks for the automatic answer.
But that's not what I was asking for.
I hope a human forummember can help me. :)

Best regards,
Wouter
The Netherlands

GreyHead

Hi Wouter,

As this code is written it will only run once when the page is loaded so there will be no values set and the value will probably be zero.

I also don't understand why you are multiplying the values by 1 ???

Please post a link to the form so I can take a quick look.

Bob

ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much

WoodyF4u

Bob,
This is the form:
http://nieuweakker.nl/index.php?option=com_chronoforms5&chronoform=inschrijfformulier
As I wrote I have no experience with javascript.
I found an example which was counting the values, so I used that for this form.
It would be great when you can If you can advise me a better script.

Best regards,
Wouter
The Netherlands

admin

Hi Wouter,

The code should be rewritten to be triggered on the change event of any of the fields referenced:

				
$("#field_id").on("change", function(){
//run calc
});


Regards,
Max

Max
If your main question got answered then please mark the answer using the button!

WoodyF4u

Hi Max,
Thanks for your support.
Which code should be rewritten and is that for every field?
Can you help me little more?
It is my first experience with js, so I hope I can learn to write my own script.
For it is a bit dificult to see the solution.

Best regards,
Wouter
The Netherlands

admin

Try this code, but JS coding requires testing and debugging, so I can't be sure that it will work:

				
window.addEvent('domready', function() {
function calc(){
if (document.getElementById){
een = document.getElementById('abo_keuze01').value;
twee = document.getElementById('abo_keuze02').value;
drie = document.getElementById('abo_keuze03').value;
vier = document.getElementById('abo_keuze04').value;
vijf = document.getElementById('abo_keuze05').value;
zes = document.getElementById('abo_keuze06').value;
document.getElementById('bedrag').value = (een * 1) + (twee * 1) + (drie * 1) + (vier * 1) + (vijf * 1) + (zes * 1);
}
}

$("#abo_keuze01").on("change", function(){
calc();
});
});

Max
If your main question got answered then please mark the answer using the button!

WoodyF4u

Thanks,
I have added the code to the Load JavaScript in the Setup event.
My form is a little bit changed now, so at page 3 I hope to see the calculated amount which is called by {bedrag}.
But I still see the text and not that calculated amount.

I have added a backup of the form here.
So I hope someone can help to fix this.

Attachments
CFV5_FormsBackup_ON_http___nieuweakker.nl_12_May_2015_22_30_14.cf5bak
(71.84 KiB)
165 Downloads/Views

Best regards,
Wouter
The Netherlands

GreyHead

Hi Wouter,

Please get help from someone with some coding experience. The whole purpose of using JavaScript is to get an immediate result when the user changes something in the form. The form you have uploaded here has nothing that can be changed on page 3. With this design you can do a calculation in PHP when Page 2 is submitted - or possibly with JavaScript in Page 2.

Bob

ChronoForms technical support
If you'd like to buy me a coffee or two, thank you very much

WoodyF4u

Ok Bob,
I will contact someone who can help me with the scripts.
Thanks for your help sofar.

Best regards,
Wouter
The Netherlands