Calculation from 3 form fields in Adobe AIR?
Hi guys,
I am a PHP developer taking my first steps into Adobe AIR and I need to make a fairly simple calculation using the values from three form fields ( can do this easily in PHP but have minimal JS experience).
Here is my body content (inc form):
-
<body>
<h1>POR Calculator</h1>
<fieldset>
<legend>Details:</legend>
<form id="por_calc" name="por_calc" method="post" action="">
<label for="cost_price">Cost Price (£):</label>
<input type="text" name="cost_price" id="cost_price" value="22" />
<br />
<label for="rrp">RRP (£):</label>
<input type="text" name="rrp" id="rrp" value="22" />
<br />
<label for="pack_size">Pack Size:</label>
<input type="text" name="unit_of_sale" id="unit_of_sale" value="6" />
<br />
<input type="submit" name="button" id="button" value="Submit" onclick="alert( calc_por( getElementById('cost_price').value, getElementById('rrp').value, getElementById('unit_of_sale').value ));" />
</form>
</fieldset>
<p id="result">place_holder%</p>
</body>
This is my PHP function (proven to work):
-
<?php
/*
* USAGE:
* echo calc_por( <cost_price>, <rrp>, <unit_of_sale> );
* <cost_price> == how much *they* buy it for
* <rrp> == how much *they* should sell it for
* <unit_of_sale> == how many are sold at a time (pack of 6 or 8 etc)...
*/
function calc_por( $cost_price, $rrp, $unit_of_sale ) {
$cost_with_vat = $cost_price * 1.175;
$result = (($rrp-($cost_with_vat/$unit_of_sale))/$rrp)*100;
return round( $result, 2 ) . '%';
}
?>
The bits JS I have done lead me to believe that PHP and JS are roughly the same language, so the function should be useful.
My questions are:
Does the function need rewriting?
How would I access the field values?
How do I call the function and output the result into <p id=result></p>?
I have chosen to use JQuery because I will probably add some fancy effects etc to it down the line (plus it is awesome)!
)