I'm making a site for myself, and I'm trying to implement some javascript for instant calculations, but I'm having some issues:
Is there any way I can avoid typing
level_prices["3"]=3; level_prices["4"]=4;
... etc. up to 90: in other words, is there any way I can do like I did in the PHP code and make it
create
this value until 90, which looks a lot cleaner?Why is this returning
$Undefined
? I suspect that it's not getting the value from the HTML form, but the code looks correct ...
Here is my code:
relevant HTML/PHP:
<form action="" id="priceCalcForm" onsubmit="return false;">
<table class="table">
<tbody>
<tr> <!-- first row, with css -->
<td style="width:50%;">
<select name="fromlevel" id="fromlevel" onchange="calculateTotal()" style="width:100%; text-align:center; font-weight:bold;">
<option value="none" name="none">0</option>
<?php
$i = 1;
while ($i < 91) {
echo '
<option value=' . $i . ' name=' . $i . '>' . $i . '</option>';
$i++;
}
?>
</select>
</td>
</tr>
</tbody>
</table>
</form>
Included JS:
var level_prices= new Array();
level_prices["None"]=0;
level_prices["1"]=1;
level_prices["2"]=2;
function getLevelPrice()
{
var levelSetPrice=0;
//Get a reference to the form id="cakeform"
var theForm = document.forms["priceCalcForm"];
//Get a reference to the select id="filling"
var selectedLevel = theForm.elements["fromlevel"];
//set cakeFilling Price equal to value user chose
//For example filling_prices["Lemon".value] would be equal to 5
levelSetPrice = level_prices[selectedLevel.value];
//finally we return cakeFillingPrice
return levelSetPrice;
}
function calculateTotal()
{
//Here we get the total price by calling our function
//Each function returns a number so by calling them we add the values they return together
var LevelPrice = getLevelPrice();
var divobj = document.getElementById('totalPrice');
divobj.style.display='block';
divobj.innerHTML = "Total Price For the Leveling $"+LevelPrice;
}
function hideTotal()
{
var divobj = document.getElementById('totalPrice');
divobj.style.display='none';
}
I consider myself decent with PHP, but its first time I am using JS.