...

View Full Version : Please help build small calculator.



AirfieldCards
04-14-2012, 07:26 PM
Hello People.

I would like to build a small calculator in php for my website.

Basically what it does is give a form to the user that looks like this
http://www.greeneru.co.uk/vince_html/img/warranty_workings.jpg

The formula is as follows.
number_from_box1
*
number_from_box2

=result1

then take result1
*52

=result2

now take 60,000
/result2

=result3

Take result3 and make it a number with only 2 decimal places
=result4

Then display the result on the form like this

Your warranty would be "result4" years

I know that there are some really good coders on here. Please could one of you talented people tell me what is the php to do the above on my site?

mlseim
04-14-2012, 07:36 PM
I would say this is javascripting, not PHP ... at least for the interactive calculation.
So you would not need to "click for warrantee". The value would adjust in real time
as the numbers are entered or changed. The button on the bottom of the form would
be to continue-on with the script, as the user would already know what the warrantee is.

Look on Google for a javascript (or JQuery) form calculator.

AirfieldCards
04-15-2012, 01:07 AM
Thanks for your reply mlseim. I'm only just getting started with PHP. Trying to learn javascript instead would be like trying to teach a fish how to ride a bike.

Please forgive me, I'm an old fart.

mlseim
04-15-2012, 02:29 AM
Save this as "test.html" and view it with your browser ...




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Warrantee Form</title>
</head>
<script type="text/javascript">
function calculateTotal(){
var h = document.getElementById("hours");
var d = document.getElementById("days");
var selectedHours = h.options[h.selectedIndex].value;
var selectedDays = d.options[d.selectedIndex].value;

var warrAnt = 60000/((selectedDays * selectedHours)*52);
var years = addDecimalPlaces(warrAnt);

//display the result
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Warrantee - Number of Years: "+years;
}
function hideTotal(){
var divobj = document.getElementById('totalPrice');
divobj.style.display='none';
}
function roundNumber(num, dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
function addDecimalPlaces(myCash) {
myCash = myCash * 1; // Confirm that myCash is a number
myCash = myCash.toFixed(2);
return myCash;
}
</script>

<style>
#wrap{
width:400px;
margin:0 auto;
text-align:left;
margin-top:8px;
padding:5px;
background:#fff;
font-family:AvenirLTStd, Arial, Helvetica, sans-serif;
font-size:13px;
line-height:16px;
}
#wrap .cont_details fieldset,.cont_order fieldset{
margin:10px;
padding:20px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
-khtml-border-radius: 10px;
border-radius: 10px;
}
#wrap legend{
font-size:16px;
font-family:Georgia, "Times New Roman", Times, serif;
color:#000;
font-weight:bold;
font-style:italic;
padding-bottom:10px;
}
#wrap .cont_details input{
margin-bottom:10px;
color:#000;
}
#wrap .input1:hover,.input1:active{

}
#wrap label{
display:block;
font-size:12px;
color:#000;
font-weight:bold;
}
#wrap label.inlinelabel
{
display:inline;
}
#wrap .cont_order input{
margin-bottom:5px;
}
#wrap .cont_order p{
padding-top:5px;
}

#wrap input[type="radio"]
{
margin-top:8px;
margin-bottom:8px;
}

#wrap input[type="text"]:hover,
#wrap input[type="text"]:active {
background-color: #FAF398;
}

#wrap input#submit
{
margin:10px;
padding-left:20px;
padding-right:20px;
padding-top:10px;
padding-bottom:10px;
}

#wrap div#totalPrice
{
padding:10px;
font-weight:bold;
background-color:#ff0;
}

#wrap label.radiolabel
{
font-weight:normal;
display:inline;
}
</style>
<body onload='hideTotal()'>
<div id="wrap">
<form action="" id="warrantee" onsubmit="return false;">
<div>
<div class="cont_order">
<fieldset>
<legend>Warrantee Calculator</legend>
<label >Hours a Day That You Work</label>
<select id="hours" name="hours" onchange="calculateTotal()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
</select>
<br/><br />
<label >Days Per Week That You Work</label>
<select id="days" name="days" onchange="calculateTotal()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<br/><br/>

<div id="totalPrice"></div>

</fieldset>
</div>

<input type='submit' id='submit' value='submit' onclick="calculateTotal()" />
</div>
</form>
</div><!--End of wrap-->

</body>
</html>





.

AirfieldCards
04-15-2012, 03:47 PM
You absolute star mlseim. That is awesome mate, cheers.
Now working in my page. http://greeneru.co.uk/warranty.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum