Forums

How to show result of calculating js in message?

WoodyF4u 24 Apr, 2015
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?
WoodyF4u 25 Apr, 2015
Thanks for the automatic answer.
But that's not what I was asking for.
I hope a human forummember can help me. 🙂
GreyHead 25 Apr, 2015
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
Max_admin 29 Apr, 2015
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, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
WoodyF4u 29 Apr, 2015
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.
Max_admin 06 May, 2015
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, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
WoodyF4u 12 May, 2015
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.
GreyHead 13 May, 2015
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
WoodyF4u 16 May, 2015
Ok Bob,
I will contact someone who can help me with the scripts.
Thanks for your help sofar.
This topic is locked and no more replies can be posted.