I’ve got this script for calculating square area, which works perfect:
<script language="javascript" type="text/javascript">
function startCalc(){
interval = setInterval("calc()",1);
}
function calc(){
one = document.autoSumForm.firstBox.value;
two = document.autoSumForm.secondBox.value;
document.autoSumForm.Area.value = (one * 1) * (two * 1);
}
function stopCalc(){
clearInterval(interval);
}
</script>
<form name="autoSumForm">
<input size="3" type=text name="firstBox" value="" onFocus="startCalc();" onBlur="stopCalc();">
<input size="3" type=text name="secondBox" value="" onFocus="startCalc();" onBlur="stopCalc();">
<input size="4" type=text name="Area" readonly="true">
</form>
But my problem is that I need the name of the third box to be Area[<?php echo $area['area_id']; ?>]
But I can’t get the javascript to work, when I use brackets in the name.
Note that you should not add the brackets if you don’t need them! It makes it unnecessary complex.
In your posted code there is no indication that you need those brackets. You normally add them if you have several input fields with the same name and you want PHP to create an array. For more information please refer to Variables From External Sources.
In case you need them, you have to use bracket notation to access the field:
Also I would suggest to not pass a value inside the brackets in the name of the field. This would simplify your code to:
and:
PHP will then create an array with continuous numerical keys starting at
0.