getting form data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xhunter
    New Member
    • May 2007
    • 42

    getting form data

    Hi,

    I am trying to use this function to get the for data :


    [CODE=javascript]function getform(divid) {
    var getstr='';
    var obj=document.ge tElementById(di vid)? document.getEle mentById(divid) : document.forms[divid];
    for (i=0; i<obj.childNode s.length; i++) {
    if (obj.childNodes[i].tagName == "INPUT") {
    if (obj.childNodes[i].type == "text") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;}
    if (obj.childNodes[i].type == "hidden") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;}
    if (obj.childNodes[i].type == "password") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;}
    if (obj.childNodes[i].type == "checkbox") { if (obj.childNodes[i].checked) {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;} else { getstr +="&" + obj.childNodes[i].name + "=off"; }}
    if (obj.childNodes[i].type == "radio") { if (obj.childNodes[i].checked) {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;} else { getstr +="&" + obj.childNodes[i].name + "=off"; }}}
    if (obj.childNodes[i].tagName == "SELECT") {var sel = obj.childNodes[i]; getstr +="&" + sel.name + "=" + sel.options[sel.selectedInd ex].value;}}
    return getstr;}
    [/CODE]

    where the id or the name of the form is sent to this function and values are returned.

    it works ok,

    but when my fields are in a table it doesn't work, it finds the "table" tag as the childnode instead of input or select.

    how can i make this work with tables?
    Last edited by acoder; Aug 10 '07, 11:17 AM. Reason: Code in tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Please post code using CODE tags. Thanks

    Show your HTML markup.

    Comment

    • xhunter
      New Member
      • May 2007
      • 42

      #3
      sure,

      I have progressed it a little bit to get the childs, but it seems it is looping around the first <td><input> found.


      let say this is the form :


      [HTML]
      <form id="form1" action="" method="post" onsubmit="retur n false;">
      <table>
      <tr>
      <td>
      <input type="text" name="var1" value="var1">
      </td>
      </tr>
      <tr>
      <td>
      <input type="text" name="var2" value="var2">
      </td>
      </tr>
      <tr>
      <td>
      <input type="submit" name="p6_Submit " value="submit" onclick="test(' form1');"/>
      </td>
      <tr>
      </table>
      </form>
      [/HTML]

      using these functions, it loops round var1 and browser locks up because of this loop

      here is the javascript code

      Code:
      function test(form) {
      var obj=document.getElementById(form)? document.getElementById(form) : document.forms[form]; 
      alert(getform(obj));
      }
      
      function getform(obj) { 
      var getstr='';
      for (i=0; i<obj.childNodes.length; i++) { 
      if (obj.childNodes[i].tagName == "INPUT") { 
      if (obj.childNodes[i].type == "text") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;} }   
      if (obj.childNodes[i].childNodes.length > 0) {getstr+=getform(obj.childNodes[i]);}
      } 
      return getstr;
      }
      as you can see, I used this to loop the childes
      [php]if (obj.childNodes[i].childNodes.len gth > 0) {getstr+=getfor m(obj.childNode s[i]);}[/php]

      This should be a very common problem, I wonder why I couldn't find anything on it.

      Comment

      • xhunter
        New Member
        • May 2007
        • 42

        #4
        Here is a full code,
        if anybody could make it work, i would really appreciate it.

        I am expecting an alert of "&p6_fullname=t est&p6_email=te st"

        [HTML]
        <html>
        <head>
        <script type="text/javascript">
        function test(form) {
        var obj=document.ge tElementById(fo rm)? document.getEle mentById(form) : document.forms[form];
        alert(getform(o bj));
        }

        function getform(obj) {
        var getstr='';
        for (i=0; i<obj.childNode s.length; i++) {
        if (obj.childNodes[i].childNodes.len gth > 0) {getstr+=getfor m(obj.childNode s[i]);}
        else if (obj.childNodes[i].tagName == "INPUT") {
        if (obj.childNodes[i].type == "text") {getstr +="&" + obj.childNodes[i].name + "=" + obj.childNodes[i].value;} }
        }
        return getstr;
        }
        </script>
        </head>
        <body>
        <form id="form1" onsubmit="retur n false;">
        <table>
        <tr>
        <td>Full Name:</td>
        <td><input type="text" name="p6_fullna me" value="test" size="40" /></td>
        </tr>
        <tr>
        <td>email:</td>
        <td><input type="text" name="p6_email" value="test" size="40" /></td>
        </tr>
        <tr><td><inpu t type="submit" name="p6_Submit " value="submit" onclick="test(' form1');"/></td></tr>
        </table>
        </form>
        </body>
        </html>
        [/HTML]

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Why not use getElementsByTa gName?

          Comment

          • xhunter
            New Member
            • May 2007
            • 42

            #6
            thanx, finally somebody,

            how would I do that, how would get the childes of an specific form?

            Comment

            • xhunter
              New Member
              • May 2007
              • 42

              #7
              thanx for pointing me to the right direction :),

              made it work.

              cheers..

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by xhunter
                thanx for pointing me to the right direction :),

                made it work.
                That's what it's all about. Glad I could be of help. Post again if you have any more problems.

                Comment

                Working...