This is kind of more of a JavaScript question, but I figure since I can't find anyone on the JavaScript boards who can help, and since it involves some PHP dynamically generated code, I'd see if anyone here has an answer...
I'm having some trouble with arrays. I'm using JavaScript to add some TEXT INPUT form fields together ONBLUR(), then the total is displayed in a READONLY TEXT INPUT field. The fields that are being added are dynamically generated using PHP and MySQL. That whole PHP part of it works great, but as a result, I need to NAME the fields quantity[0], quantity[1], quantity[2]...
From what I know about JavaScript, this shouldn't be a problem. But it is.
Here's my original code:
<?php
if ($qty == '0')
{echo ""; }
else
{ echo ("<script language=JavaScript>
<!-- Hide
var quantity=new Array(".$qty.")\n");
for ($i = 0; $i < $qty; $i++)
{ echo "quantity[".$i."]=0;\n"; }
echo ("function Calculate()
{ document.locationsform.total_qty.value = (0");
for ($i = 0; $i < mysql_num_rows($result); $i++)
{ echo " + parsefloat(document.locationsform.quantity[".$i."].value)"; }
echo ("); }
// End -->
</script>");
}
?>
<form method="POST" action="in-home-date.php" name=locationsform><table>
<?php
if ($qty == '0')
{echo "<tr><td><font face=arial size=2>At present your address book
is empty. In order to place locations on your <b>".$piece."</b>,
add a
location to your address book using the form to the right.</font></td></tr>"; }
else
{ for ($i = 0; $i < mysql_num_rows($result); $i++)
{ $row = mysql_fetch_row($result);
{ $location_id = $row[0];
$name = $row[1]; }
?>
<tr>
<td align=left nowrap><font face="Arial" size="1"><input type=checkbox
name="location_id[<?php echo ($i); ?>]" value=<?php echo ($location_id); ?>
style="font-family: Arial, sans-serif; font-size: 7pt"></td>
<td align=left nowrap>edit delete</td>
<td align=left width=99% nowrap> Specific Address from database</td>
<td align=right nowrap><font face="Arial" size=1>Quantity: </font></td>
<td align=left nowrap><input type=text size=6 value=0
name="quantity[<?php echo ($i); ?>]" <?php if ($qty != 0)
{ echo "onBlur=\"Calculate();\""; } ?>></td>
</tr>
<?php
}
}
?>
</table>
<p align=center><b>Total Qty: </b><input value=0 type=text
size=7 name="total_qty" readonly> <input type="submit"
value="Submit Locations" /> <input type="reset"
value="Clear" /></p>
And here's my dynamically generated script:
<script language=JavaScript>
<!-- Hide
var quantity=new Array(8)
quantity[0]=0;
quantity[1]=0;
quantity[2]=0;
quantity[3]=0;
quantity[4]=0;
quantity[5]=0;
quantity[6]=0;
quantity[7]=0;
function Calculate()
{ document.locationsform.total_qty.value = (0 +
parsefloat(document.locationsform.quantity[0].value) +
parsefloat(document.locationsform.quantity[1].value) +
parsefloat(document.locationsform.quantity[2].value) +
parsefloat(document.locationsform.quantity[3].value) +
parsefloat(document.locationsform.quantity[4].value) +
parsefloat(document.locationsform.quantity[5].value) +
parsefloat(document.locationsform.quantity[6].value) +
parsefloat(document.locationsform.quantity[7].value)); }
// End -->
</script>
...and the error message that I'm getting when running the function Calculate() says:
"document.locationsform.quantity.0 is null or not an object"
It seems to be reading the array object number as the value of the field "quantity," a field that, of course, does not exist anywhere in my code.
Did I declare something wrong? What's going on?