Multiple data sets in a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hkma08
    New Member
    • Mar 2008
    • 11

    Multiple data sets in a form

    For certain reason, I need to check the validity of a form when submit with many data with shared name. The form looks like this:

    [HTML]<form id="upload_form " name="upload_fo rm" onsubmit="retur n uploadCheckVali d2()">
    <table>
    <tr>
    <td>xxx</td>
    </tr>
    <tr>
    <td>
    <input type="text" name="title[]" value="10" />
    </label></td>
    ....
    ....
    </form>[/HTML]

    For the js:
    [CODE=javascript]function uploadCheckVali d2()
    {
    var titles = document.forms[0].elements['title'][0].value;
    alert(titles);
    if(xxx != xxx)
    return false;
    }[/CODE]

    I don't even able to alert the nodeName or value. Could anyone help??
    Last edited by gits; Mar 17 '08, 07:53 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    give your input-nodes an id and refer with:

    [CODE=javascript]var node = document.getEle mentById('the_i d');[/CODE]
    kind regards

    Comment

    • hkma08
      New Member
      • Mar 2008
      • 11

      #3
      i have 10 inputs per table, and will have 5 tables in max. So your way works but will be very long in code. Any other ways?

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        do you mean you want to check all input nodes for the same thing? then just use:

        [CODE=javascript]document.getEle mentsByTagName( 'input');[/CODE]
        and check for the type == text and its values ...

        kind regards

        Comment

        • hkma08
          New Member
          • Mar 2008
          • 11

          #5
          Although the name attached to me is newbie, i don't think this is that easy to solve, and probably this is advance stuff.

          May be i should explain more further on what i want to do. Did you notice that the name i gave to the input has braces? such as name="title[]". I have a table with following inputs:

          title, author, isbn, post date etc...

          when i hit an 'add' button, the table is duplicated with same structure, including names and ids. And in order to save to database by php, i have to use name="title[ ]" instead of just name="title" (w/o braces), since there is multiple record sets. When i was doing w/o multiple data sets, i used:

          document.forms[' myform '][' title ']
          or other ways such as the way you mentioned.

          I tried and stuck on that the following ways do not work at all:
          document.forms[' myform '][' title[ ] ']
          or for (i=0;i<5;i++) { document.forms[' myform '][' title '][ i ] .......... }

          simple example is that i have several isbn, all used share name, isbn[], the data pass to php is array of data, i am able to extract it to php, but not able to check every isbn is 10 or 13 digits (according to international book numbering) by javascript.

          Get it? Excuse me for this complicated problem. But I am getting crazy on this ........ Please help if possible.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            How about document.getEle mentsByName("ti tle[]") ?

            Comment

            • hkma08
              New Member
              • Mar 2008
              • 11

              #7
              i tried this, doesn't work either...

              var form = document.getEle mentById(formid );
              var titles = form.getElement sByName('title[]');
              alert(titles[0].value);

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                getElementsByNa me is a method of the Document object. Try:
                [code=javascript]var titles = form.elements['title[]'];[/code] instead.

                Comment

                • hkma08
                  New Member
                  • Mar 2008
                  • 11

                  #9
                  oh, i finally got it in some way.

                  [CODE=javascript]function checkBookUpload Valid(theForm)
                  {
                  var inputs = theForm.getElem entsByTagName(' input');
                  for (var i=0; i<inputs.length ; i++)
                  {
                  if (inputs[i].name == 'title[]')
                  {
                  if(inputs[i].value == '') {inputs[i].style.backgrou nd = error_color; return false;}
                  else {inputs[i].style.backgrou nd = 'white';}
                  }
                  .....
                  .....
                  .....
                  }
                  [/CODE]
                  This way works good. But still thanks to you guys trying to answer me..
                  Last edited by gits; Mar 26 '08, 10:09 PM. Reason: added code tags

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    That's similar to what gits suggested, but anyway I'm glad you've managed to solve it.

                    Comment

                    • hkma08
                      New Member
                      • Mar 2008
                      • 11

                      #11
                      oh it is!
                      probably i was so stuck on something and misunderstand what he said.
                      my bad.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Hey, no problem. At least it's working and that's what we're here for.

                        Comment

                        Working...