Results 1 to 5 of 5

Thread: alert function using arrays

  1. #1
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default alert function using arrays

    I have a form that when it is submitted it displays an alert box

    I want that alert box to display the value of the fields that were submitted

    e.g.

    HTML Code:
    <form action="somepage.php" name="orderqty" onsubmit="alert('you entered ' + orderqty.qty.value + '!')">
    <input type="text" name="qty" />
    <input type="submit" value="submit" />
    </form>
    This works fine, however the form that I am submitiing has multiple input boxes with the same name

    e.g.
    HTML Code:
    <input type="text" name="qty[]" />
    <input type="text" name="qty[]" />
    <input type="text" name="qty[]" />
    <input type="text" name="qty[]" />
    <input type="text" name="qty[]" />
    <input type="text" name="qty[]" />
    Is there anyway I can get the alert function to read the values of all the qty[] input fiels added together?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <form action="somepage.php" name="orderqty" onsubmit="
    for (var n=0, e=this.elements, q='you entered:\n', i = 0; i < e.length; i++)
    if (e[i].name && e[i].name=='qty[]')
    q+=(++n)+': '+e[i].value+'\n';
    alert(q);">
    <div>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="submit" value="submit">
    </div>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Works great, thank you.

    But how do I add the values together rather than displaying them line by line
    as there could be upto a hundred lines at a time

    e.g.
    instead of:
    1 : 3
    2: 8
    3: 14


    i would like

    25

    This is hopefully straight forward as it is basic maths but I dont know how to code it as I don't really know javascript.

    Thanks
    Last edited by jc_gmk; 11-23-2007 at 04:40 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    
    </head>
    <body>
    <form action="somepage.php" name="orderqty" onsubmit="
    for (var q=0, e=this.elements, i = e.length-1; i > -1; --i)
    if (e[i].name && e[i].name=='qty[]' && !isNaN(+e[i].value))
    q+= +e[i].value;
    alert('you entered: '+q);">
    <div>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="text" name="qty[]"><br>
    <input type="submit" value="submit">
    </div>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    May 2007
    Location
    England, UK
    Posts
    235
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default

    Perfect, thanks once again!!

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
  •