View Full Version : form arrays

02-09-2006, 06:52 PM
i've used php to dynamically generate forms:

echo "<input class='small' type='text' name='quan[".$count."]' size='1' value='0' onKeyUp='addtotal($pack)';> ";

the form is called prodform, and the input is called quan[0], quan[1], quan[2] as $count increases.

here's my javascript:

function addtotal(pack) {
var totalprice = 0.0
var i = 0
while (document.prodfam.quan[i]) {
if (document.prodfam.quan[i].value)
totalprice += parseInt(document.prodfam.quan[i].value) * parseFloat(document.prodfam.price[i].value)
document.prodfam.quan[i].value = 0
document.prodfam.subtotal.value = roundit(totalprice)
I'm getting an error everytime I use document.prodfam.quan[i] saying document.prodfam.quan is null or not an object.

I've tried document.prodfam.elements[i], document.forms[0].elements[i] and still no luck. What's going on? Is this not possible?

02-09-2006, 07:14 PM
The problem with your approach is that you name the fields "quan[n]" but you are tryin to access item n of the field named "quan".

This could easily be handled by using the following access method:

A thing to note, however:
Text controls shouldn't ever be given the same name as any other control in the form. Only radio and checkbox types of controls are allowed to share names with other controls. A browser may simply ignore all but the first control if you give other types of controls the same name. (In practice no browser does handle it that way, but if there ever arrives a browser that does, it's not doing anything wrong and if anything breaks it's your fault and not the browser's.)

02-09-2006, 08:39 PM
thanks, that just about fixed it.
In case others are following this,


didn't work, but



As far as naming form elements the same name, isn't that the point of accessing them in an array? In other words which method is better, if the number of elements depends on info from the database:

quan1, quan2, quan3...
quan[1], quan[2], quan[3]...

it seems to me the second method is easier to access in php, but maybe i'm not thinking correctly here.

Also, does anyone have a link to a page that lists all objects in an html file, in xml style or something? so I can see a tree of all my object arrays?

02-09-2006, 09:14 PM
First of all, you have to realise that php differs in what it means by giving a form control the name "quan[n]". While for php it means the element is the nth element in an array of "quan" elements, what it really means according to the specification is just the single name "quan[n]". This control has nothing at all to do with that other form control that is named "quan[m]". The array it entirely a php construct, and doesn't exists from other handlers of the form.

However, consider if you had not added n there. If you had given the controls all the same name of "quan[]" which, if my memory doesn't fail me, also does the exact same thing from php's point of view. This is the case I'm warning you about, because of the fact that only checkboxes and radio buttons are actually allowed to share name with other form controls.

As for giving the names "quan[n]" versus "quann", it doesn't matter at all on the client side. But since one of them is treated more as you want it server side, that one makes more sense overall.

02-12-2006, 08:09 PM
have a look at scripts F45 f46 f47

on how to handle []