SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Javascript array from form

    Hi all

    Cant seem to get this working how I want it, any help appreciated

    Ive a realtime input form (users can add extra lines using a javascript function) that needs to return to the database on submit, this works fine. in the next section of the form I have need to read the values put in the first section for inclusion in a select box, again using JS.

    Al lthe abouve work fine providing that I use the variable[] for the name of the forms elements etc.

    however I really need to use the format variable[x][y] and thats where everything stops working.


    So, in section 1 of my form that the javascript writes I have

    Code:
       newCell2.innerHTML = '<input type="hidden" name="building[' + (numRows) + '][id]"  ' + (buildid) + ' /><input type="text" name="building[' + (numRows) + '][name]" ' + (buildingname) + ' />';
    numRows = the current row number of the table, it counts up as people add new lines to the form.

    buildid & buildingname = text strings that add a value to the input tags.

    then in my second section Im trying to read in the values of these arrays using.

    Code:
    buildingarr = document.getElementsByName("building[]");
      opts = null;
      for(var i=0;i<buildingarr.length;i++){
        opts = opts +'<option value="' + (buildingarr[i][name].value) +'">' + (buildingarr[i][name].value) +'</option>';
       } 
        newCell5.innerHTML = '<select name="room[' + (numRows) + '][building]">' +
             opts +
            '</select>';
    TIA
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Don't reference the form fields using the name attribute from JavaScript - reference them by their id instead and then you can use whatever the server side processing need the name to be without it interfering with the JavaScript.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Member
    Join Date
    Aug 2012
    Location
    Taiwan
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code could be hard to maintain. Since you intended to gather all the input value for the options.
    Just go for it, and later you can filter node attributes to build your select element.

    buildingarr = document.querySelectorAll("#section1 input[type=text]");
    for (var i = 0, len = buildingarr.length; i < len; i++) {
    var value = buildingarr[i].value;
    opts = opts + '<option value="' + value + '">' + value + '</option>';
    }

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by luc.tw View Post
    opts = opts + '<option value="' + value + '">' + value + '</option>';
    }
    Don't try to add options to a select that ewat - use new Options(txt,val) instead
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Don't try to add options to a select that ewat - use new Options(txt,val) instead
    new Option(txt,val)
    Tab-indentation is a crime against humanity.


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
  •