Advanced Search

Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: grabbing all form data

  1. #1
    Join Date
    Jul 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default grabbing all form data

    i was wondering if anyone knew a way where i could grab all the form data of a bunch of fields inside the form all at once? is there a way to easily and quickly do this?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,699
    Thanks
    43
    Thanked 3,132 Times in 3,096 Posts
    Blog Entries
    12

    Default

    Those values are already available as a part of the form's elements collection. There is no real need to 'gather' them. But, if you must:

    Code:
    var a=[];
    for (var i = 0, e=form.elements; i < e.length; i++)
    a[i]=e[i].value;
    where form represents a reference to the form in question. This will give you an array of the values named a. If we knew the specifics of the project, especially what you wanted to do with the data, we could probably be more helpful.

    Technically though, a form element, even if it has a value, has no data if it has no name. The above would gather information even from form elements with no name. But, it all depends upon what you want to do.
    - John
    ________________________

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

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    Note that if you are collecting this data serverside, you could just use $_POST (or $_GET). Might be better, in the end, depending on your goals.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    function formToObject(frm) {
      for(var i = 0, r = {}, f = frm.elements, n = f.length; i < n; ++i)
        if(f[i].name)
          if(!r.hasOwnProperty([f[i].name]))
            r[f[i].name] = f[i].value;
          else if(r[f[i].name] instanceof Array)
            r[f[i].name].push(f[i].value);
          else
            r[f[i].name] = [r[f[i].name], f[i].value];
      return r;
    }
    Last edited by Twey; 11-04-2007 at 05:25 PM. Reason: Wrong index variable used.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,699
    Thanks
    43
    Thanked 3,132 Times in 3,096 Posts
    Blog Entries
    12

    Default

    There is also (when using javascript to do this) the matter of - say radio buttons that each may have different values, but the same name, and one or none of them may be checked. The data (value) for that name would technically be empty if none were checked and be only that of the checked button if one is checked.

    With Twey's code (which he may edit if I am right about this), I don't see how incrementing i would do anything to change n, resulting in only one element being processed over and over. I may have missed something.
    - John
    ________________________

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

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You're quite right. I was writing it on autopilot, and I think I may have become sidetracked with some thought of mathematical functions 'f(n)' or such.

    The radio/check boxes are easily solved:
    Code:
    function formToObject(frm) {
      for(var i = 0, r = {}, f = frm.elements, n = f.length; i < n; ++i)
        if(f[i].name && f[i].checked !== false)
          if(!r.hasOwnProperty([f[i].name]))
            r[f[i].name] = f[i].value;
          else if(r[f[i].name] instanceof Array)
            r[f[i].name].push(f[i].value);
          else
            r[f[i].name] = [r[f[i].name], f[i].value];
      return r;
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,699
    Thanks
    43
    Thanked 3,132 Times in 3,096 Posts
    Blog Entries
    12

    Default

    Cool, I mean I haven't tested it, but it looks great now. Except, why declare n if it is only used once?
    - John
    ________________________

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

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's a tiny optimisation tweak. Accessing a .length property is actually relatively computationally expensive, it's quite a bit cheaper to access it once and then store it if you know the length isn't going to change during the loop.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,699
    Thanks
    43
    Thanked 3,132 Times in 3,096 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    It's a tiny optimisation tweak.
    I understand the concept. If:

    Code:
    i < n
    were to be evaluated during every loop, it would be (if applied to all such looping arguments that have no chance of n modulating during any given set loop instance) a minor to significant resource savings, depending upon the number of times such a construct could be substituted for the more normal/conventional (in this case):

    Code:
    for(var i = 0, r = {}, f = frm.elements; i < f.length; ++i)
    I'm just not sure that any, or many javascript parsing engines would 'give a rat's a**', that is , treat the loop any differently, as far as resource requirements are concerned, and, if so, some or all might require slightly more resources.

    Usually, the more code you write, however well intentioned, simply requires more parsing and/or processing.

    Even when it doesn't, it is often a ratio to the number of times you use the extra code. The more times 'n' is used, the more of a savings it represents (if it still represents its original object/value. Is 'n' in this case really being reused more efficiently than f.length would be? I'm not convinced, but it could be. I would feel more confident if 'n' were defined before the loop began.

    Even then, the declared definition might need to be evaluated with each loop.

    In another matter, as nice as the code looks (it looks great!), and my above speculations with or without standing, I still haven't gotten around to testing it, and who knows if it (or any of the other suggestions in this thread) solves the original problem posted by ATMA. Oh, ATMA could tell us perhaps
    - John
    ________________________

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

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Even when it doesn't, it is often a ratio to the number of times you use the extra code. The more times 'n' is used, the more of a savings it represents (if it still represents its original object/value. Is 'n' in this case really being reused more efficiently than f.length would be? I'm not convinced, but it could be. I would feel more confident if 'n' were defined before the loop began.
    You're missing the point. It's not about how it's reused: the .length property calculates the length of the array on access. Since we know the array's length isn't going to change here, we can avoid that overhead by only calculating it once and storing it in n.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •