Click to See Complete Forum and Search --> : array assistance


timandkitty
01-10-2003, 08:21 AM
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 dynamically generated code:



<form method="POST" action="in-home-date.php" name="locationsform">


<script language=JavaScript>
<!-- Hide
var quantity=new Array(3)
quantity[0]=0;
quantity[1]=0;
quantity[2]=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)); }
// End -->
</script>


<input type=text size=6 value=0 name="quantity[0]" onBlur="Calculate();">

<input type=text size=6 value=0 name="quantity[1]" onBlur="Calculate();">

<input type=text size=6 value=0 name="quantity[2]" onBlur="Calculate();">

<b>Total Qty: </b><input value=0 type=text size=7
name="total_qty" readonly> &nbsp;
<input type="submit" value="Submit Locations" /></form>



...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?

AdamBrill
01-10-2003, 09:30 AM
Try this code:

<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<form method="POST" action="in-home-date.php" name="locationsform">
<script language=JavaScript>
<!-- Hide
Elements=document.getElementsByTagName("input");
function Calculate()
{
Elements[3].value = Number(Elements[0].value) + Number(Elements[1].value) + Number(Elements[2].value);
}
// End -->
</script>
<input type=text size=6 value=0 name="quantity[0]" onBlur="Calculate();">
<input type=text size=6 value=0 name="quantity[1]" onBlur="Calculate();">
<input type=text size=6 value=0 name="quantity[2]" onBlur="Calculate();">
<b>Total Qty: </b><input value=0 type=text size=7 name="total_qty" readonly> &nbsp;
<input type="submit" value="Submit Locations" /></form>
</body>
</html>

That should work for you. BTW, I like your signature! :)

Webskater
01-10-2003, 09:31 AM
First I assume your javascript function is not actually between the form tags.
Second, I don't think Javascript will like control names like quantity[] - an array element will be expected.
The easy way to do this is to put your 3 input boxes and your TotalQuantity box in 4 separate cells in a row in a table. Lets say you put Quantity1 in the first cell, Quantity2 in the second, Quantity3 in the third cell and the TotalQuantity box in the fourth cell. Then:
function calculate()
{
var row = event.srcElement.parentElement.parentElement;
var Qty1 = new Number(row.cells(0).firstChild.value);
var Qty2 = new Number(row.cells(1).firstChild.value);
var Qty3 = new Number(row.cells(2).firstChild.value);

var total = Qty1 + Qty2 + Qty3;

row.cells(3).firstChild.value = total;
}

gil davis
01-10-2003, 09:35 AM
Your text field names include braces. You would have to index them using a string:

document.locationsform["quantity[0]"]

Alternately, drop the braces out of the name:

<input type=text ... name="quantity" ...>

This makes an array of text boxes for you. Then you can access them using:

document.locationsform.quantity[0]

Your choice.

timandkitty
01-10-2003, 09:43 AM
Thanks for the many great suggestions, guys!
I posted this on some other boards as well and got virtually no response.

I finally went with this thought just a few minutes ago:
document.locationsform["quantity[0]"]

Thanks again!