Create aritmetic operation on change field value

dgidgio 19 Sep, 2011
Hi people

I have 3 selected box:

one with options: 1 or 2 or 3
other with options: 0 € or 10 €
Other with options: yes or no

I want create a calculate operation when change the value of the selected boxes.

((1 or 2 or 3) * 10) + (0 or 10) + (if(yes): 10 else 0)

How to create this before submit form? In event onchange selected box?

Thanks
GreyHead 19 Sep, 2011
Hi dgidgio ,

Most often I'd expect to see the calculation done using JavaScript in the form itself so that the user sees the result before submitting the form. Or you can do it after the form is submitted using PHP in the On Submit Before Email box (in CFv3) or a Custom Code action (in CFv4).

Bob
?
Guest 19 Sep, 2011
Hi

I have a similar problem. I have a simple text field (a number) and I would like to do some arithmetic operations on it, after submitting. Unfortunately I cannot get the field value passed from the form.

Here the form code for the input text:
<input id="consumo" maxlength="10" size="10" class=" validate['required','digit']" title="" label_over="0" hide_label="0" type="text" value="" name="consumo" />

I tried to get the content of the text field with $consumo ($variable_name) or $_POST['consumo'] but it doesn't work. I got always the following error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\xampp\htdocs\joomla16\administrator\components\com_chronoforms\form_actions\custom_code\cfaction_custom_code.php(14) : eval()'d code on line 1

Notice: Undefined variable: consumo in E:\xampp\htdocs\joomla16\administrator\components\com_chronoforms\form_actions\custom_code\cfaction_custom_code.php(14) : eval()'d code on line 1
prova custom comde
Powered By ChronoForms - ChronoEngine.com


Any idea what could be wrong ?

Thanks a lot
Best regs.
gp
GreyHead 19 Sep, 2011
Hi jemp,

I deleted your second post because it seemed to be a duplicate - though I see that the error line number was 2 instead of 1 here.

There's a problem with the code in your Custom Code action. Possibly you missed out the <?php tags. Try
<?php
$consumo = $form->data['consumo'];
...
?>

Bob
dgidgio 19 Sep, 2011
I change the readonly property:

<SCRIPT LANGUAGE="javascript">
function calcular() 
{
   document.inscricao_2011.txtValor.setAttribute('readonly','readonly');
}

onLoad=calcular();


</SCRIPT>


How to use this when the combobox cmbAlmoco change?
GreyHead 20 Sep, 2011
Hi dgidgio,

Please try this in the Form JS box (in CFv3) or a Load JS action (in CFV4):
window.addEvent('domready', function() {
  $('cmbAlmoco').addEvent('change', function() {
    $('txtValor').readOnly = true;
  });
});
Note the $('txtValor') and $('cmbAlmoco') references are to input ids, not input names.

Bob
dgidgio 20 Sep, 2011
Not work. No problem. Work in onLoad event.

I need create a onChange event to do:

I want create a calculate operation when change the value of the selected boxes.

((1 or 2 or 3) * 10) + (0 or 10) + (if(yes): 10 else 0)

How to create this before submit form? In event onchange selected box?
GreyHead 20 Sep, 2011
Hi dgidgio,

JavaScript usually goes best in the Form JS box (in CFv3) or a Load JS action (in CFV4).

Bob
dgidgio 20 Sep, 2011
Problem solved


<SCRIPT LANGUAGE="javascript">

function calcular()
{

document.inscricao_2011.txtValor.setAttribute('readonly','readonly');


var acompanhantes = parseInt(document.inscricao_2011.cmbAcompanhantes.value) * 12;

var almoco = parseInt(0);



if(document.inscricao_2011.cmbAlmoco.value == 'N')
{
almoco = parseInt(0);
}
else
{
almoco = parseInt(10);
}

var total = parseInt(10) + acompanhantes + almoco;

document.inscricao_2011.txtValor.value = total;


}

onLoad=calcular();
document.inscricao_2011.cmbAlmoco.onchange=function(){calcular()};
document.inscricao_2011.cmbAcompanhantes.onchange=function(){calcular()};


</SCRIPT>
This topic is locked and no more replies can be posted.