constructing form parameters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rameshch45
    New Member
    • Oct 2007
    • 25

    constructing form parameters

    In a javascript method, I have a form name.

    Given that form name, I want to be able to read all its paramter names and their values. and construct in the form of "name="+encodeU RI("value")+"& " and so on so that I can pass the final string to the ".send" method of xmlhttp.

    Is there a helper function that does this already? Or which functions should I use to iterate through any given form with just its name.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use the elements array and loop over it to get all the elements. If you have different types of elements, e.g. text fields, checkboxes, radio buttons, select drop-downs, etc., you will need to deal with them differently to get the values and simulate a GET/POST form submission.

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5390

      #3
      to give one more hint on how you could do that ... just create a function like this:

      [CODE=javascript]/**
      * retrieves the values of nodes within a base-node
      * @param base_node node-ref
      * @return obj object with corresponding key-value-pairs
      */
      function get_values(base _node) {
      // just a list with relevant fields - adapt it to your needs
      var tags = { input: 1, select: 1 };
      var obj = {};

      // we loop through the tag list here
      for (var i in tags) {
      var nodes = base_node.getEl ementsByTagName (i);

      // we loop through the node list - actually without checking for
      // type == 'text' or whatever
      for (var j = 0, n; n = nodes[j]; j++) {
      obj[n.name] = n.value;
      }
      }

      return obj;
      }
      [/CODE]
      so with that you could start to build a next function that builds a query-string for you with the returned obj, or build it into the shown method and return the query-string here instead of the obj ...

      good luck ... :)

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Nice! However, it would need to be adapted to allow for radio buttons and checkboxes since you only want checked buttons/boxes, and multiple select elements (where you only want selected options).

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5390

          #5
          Originally posted by acoder
          Nice! However, it would need to be adapted to allow for radio buttons and checkboxes since you only want checked buttons/boxes, and multiple select elements (where you only want selected options).
          that's right :) ... the function would need handling for different input-types and or attributes of elements that could be of interest ... but with such a function you would always have the handling of the ddocument's content in one place ... so antime you need another node handled you just could adapt it here ...

          it is a typical framework-function and i assume that everything that calls itself a javascript(ajax )-framework has something like that implemented :) ... or at least it should have ...

          kind regards

          Comment

          Working...