Forums

Display a calculation

Tiw 24 Mar, 2012
Hi

I would like to display the result of a simple calculation like

field1*30+field2*40

I've tried this code :
window.addEvent('myform', function() {
  $('field1').addEvent('change', calculate);
  $('field2').addEvent('change', calculate);
});
function calculate() {
  var sum = parseInt( $('field1').value ) + parseInt( $('field2').value );
  if ( isNaN(sum) ) {
    sum = '';
  }
  $('box3').value = sum;
};

found it in the forum..

got field1 and 2 as text boxes, and box3 as a text box.

when debugging I get this message :


Data Array:
Array
(
[option] => com_chronoforms
[chronoform] => myform
[event] => submit
[Itemid] =>
[2sidd] => 1
[input_text_2] => 2
[input_text_3] =>
[input_submit_1] => Submit
[46b198acc9ea0488e3b22a0195ce67df] => 1
)
Validation Errors:
Array
(
)

I would like to have the result to be displayed in "box3" either when hitting submit or on changes made to field 1 or 2... any suggestion?

thanx
GreyHead 25 Mar, 2012
Hi Tiw,

You can't grab random code snippets and expect them to work on your form HTML by magic.

In the code snippet $('field1') refers to 'the input with an id of field1'; none of your inputs have the name field1 so it's a fair bet that they don't have the input field1.

Similarly the snippet writes the answer to an input with the id set to box3.

Bob
Tiw 25 Mar, 2012
Hi Greyhead!

Thanks for your reply, I managed to solve the problem🙂

I have another question for you.

If I want a value to follow from one form to another and then autofill the default value of a text area on the other page, how do I do that?

for example, if I have an order form in page 1 and the customer fills in 1 red apple in my text box "box1" can I then load that value into a text area in the email form after they hit submit on the order form?

Thanks
GreyHead 25 Mar, 2012
Hi Tiw ,

If it's in the URL or the post data from a previous form (i.e. this form URL was the action URL of the previous form) Then it will be automatically loaded into the $form->data array and loaded into your form.

If you use a Session to Data action on the previous form and a Data to Session action on this form AND match the session keys it will also be automatically loaded.

If you save the data to a database redord in the previosu form and retrieve the same record in this form then it will load.

There are other ways but those are the most common.

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