tags:

views:

20

answers:

2

I need to create a simple form which asks two values from the user, and when she submits the form shows a value calculated based on those values.

For example, user could be shown something like this:

1st number:
2nd number:
Result:
Calculate!

The only editable fields are 1st and 2nd number. After giving the numbers and clicking Calculate!, she sees the following page:

1st number: 15
2nd number: 20
Result: 35
Calculate!

She can now modify the numbers and reclick Calculate! It is OK to submit and reload the page, the values don't need to be updated in-place.

Do I have to do this using JavaScript injection, or is there another way?

A: 

You don't have to do something like this with JavaScript, but that would be the quickest and easiest solution. Especially since you have jQuery available. You don't even have to let users click calculate.

example code:

$("#input_1, #input_2").change(function(){
  $("#result").text(parseInt($("#input_1").val(), 10) + parseInt($("#input_2").val(), 10));
});

given two input fields with id input_1 and input_2 and a span/div etc. to display the result with id result the above code automatically calculate the two values whenever something is new is entered into them.

googletorp
A: 

Take a look at Computed Field module.

frjo

related questions