Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: form arrays

  1. #1
    New to the CF scene
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    form arrays

    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:

    Code:
    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)
      else
    	document.prodfam.quan[i].value = 0
      i++
    }
    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?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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:
    Code:
    var
        oControl=document.forms["prodform"].elements["quan[n]"];



    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.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, that just about fixed it.
    In case others are following this,

    document.forms["prodfam"].elements["quan[i]"]

    didn't work, but

    document.forms["prodfam"].elements["quan["+i+"]"]

    worked.

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

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,518
    Thanks
    3
    Thanked 506 Times in 493 Posts
    have a look at scripts F45 f46 f47

    on how to handle []

    http://www.vicsjavascripts.org.uk/Fo...Compendium.htm


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •