I've found lots of useful help in previous questions, but can't seem to get the last piece I need. I use PHP to loop through form fields and uniquely name the fields (using a variable). One of my fields is an "expense type". Another is "expense amount". I'd like the expense amount to auto display a mileage rate if "mileage" is the expense selected. I can do this outside of the array, but in the array I'm having trouble getting the names right in the array. The PHP code and the rest of the form work perfectly, just the javascript isn't finding the right fields to trigger/update. Here's the code:
<?php
$t = -1;
foreach($details as $detail)
{
$t++;
echo '<th><select name="record['.$t.'][expense]" onChange="mileage();" ><option value="">Expense</option>';
foreach($expensecoderesults as $code)
{
echo '<option value="'.$code->expense_code_id.'"';
if($code->expense_code_id == $expense_type_id){echo 'selected="selected" ';}
echo '>'.$code->expense_code_name.'</option>';
}
echo '</select></th></tr>';
echo '<th><input type="number" "step=".01" name="record['.$t.'][amount]" /></th>';
}
?>
<script language="javascript" type="text/javascript">
function mileage()
{
var myForm = document.forms.edit_emp_exp;
var myControls = myForm.elements['record[][expense]'];
var myAmount = myForm.elements['record[][amount]'];
for (var i = 0; i < myControls.length; i++)
{
if(myControls.value == 28)//28 is the id of "mileage" expense
{myAmount.value=".54";}
}
}