SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    accessing <input name="myedit[]"> with only one element

    Hi,

    I have a form that is generated dynamically with php. It can have one or more blocks of fields which I have defined as arrays.

    Example:

    HTML Code:
    <form name="book" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input name="price[]" type="text" value="10">
    <input name="price[]" type="text" value="20">
    <input name="price[]" type="text" value="30">
    And then I process the form with PHP and it works beautifully.

    But I also have a discount button that calculates a new price in javascript. And accessing the input field arrays using the following only works if there are more than one element in the array. IE if I have just one "price" input field, it doesnt work. If I have 2 or more, it works like a dream.

    Code:
    document.forms["book"].elements["price[]"][0].value
    I guess it just isn't considered an array in javascript if there is only one element.

    Again, this all works fine as far as the processing in php, no matter how many "price" input fields I have, even just one.

    Does anyone know of a work around for this?

    I suppose I could code it to use name="price1", name="price2", etc but I was hoping to use arrays. Much cleaner.

    Thanks!

  2. #2
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Italy
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, this is how I have "solved" this. It is hokey hokey hokey. I am posting it just to give more info on the problem, not because I see this as a real solution.

    If there is just one "price" input field, then document.forms["book"].elements["price[]"].length is "undefined", otherwise, if there are 2 or more input fields, it behaves properly.

    So I do this check and access the input fields based on this:

    Code:
    if(document.forms["book"].elements["price[]"].length > 1)
        calcPrice(document.forms["book"].elements["price[]"][0].value);
    else
        calcPrice(document.forms["book"].elements["price[]"].value);
    Any ideas on a better way to do this?

    Thanks!

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The name of the element doesn't need to have [] to be an array...
    If you use <input name="price" value="10"><input name="price" value="20">, then
    document.forms["form"].elements["price"]
    will still return an array (this is just a FYI).

    Your code isn't that hokey You could just use
    if( document.forms["form"].elements["price"].length ) {
    blah;
    } else {
    blah;
    }
    but your way will be ok too.


Bookmarks

Posting Permissions

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