SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Add extra images

    Hi,
    I m new to JavaScript. And I have a webpage where a user can upload 5 images. so I have five input tags, but I want to give him add extra images option,when a user hit add extra image a new input tag appeare.
    My HTML code looks like this
    Code:
       <form action="#" enctype="multipart/form-data" method="post" id="imagesForm">
          <table id="imagesTable">
          <tr>
           <td width="150">foto 1:</td>
           <td width="250"><input type="file" name="foto1" size="30" /></td>
          </tr>
          <tr>
           <td width="150">Description:</td>
           <td width="250"><input type="text" name="description1" size="30" /></td>
          </tr>
        </table>
        <input type="button" onclick="addInput('imagesTable')" value="Upload Another" />
        <br /><br />
          <input type="submit" value="Upload" name="submit" />
      </form>
    Here is my code
    Code:
    function addInput(parentID)
        {
          var parent  = document.getElementById(parentID);
          // the first row
          newInputFile = document.createElement('input');
          newInputFile.type = 'file';
          newInputFile.name = 'foto2';
          newInputFile.size = '30';
          var parent  = document.getElementById(parentID);
          var txt1 = document.createTextNode("foto2");
          var row1  = document.createElement('tr');
    
          var td1 = document.createElement('td');
          td1.setAttribute("width","150");
          var td2 = document.createElement('td');
          td2.setAttribute("width","250");
    
    
          //second row
          newInputTxt = document.createElement('input');
          newInputTxt.type = 'text';
          newInputTxt.name = 'description2';
          newInputTxt.size = '30';
          var txt2 = document.createTextNode("Description2");
          var row2  = document.createElement('tr');
          var td3 = document.createElement('td');
          td3.setAttribute("width","150");
          var td4 = document.createElement('td');
          td4.setAttribute("width","250");
    
          parent.appendChild(row1);
          parent.appendChild(row2);
          row1.appendChild(td1);
          row1.appendChild(td2);
          row2.appendChild(td3);
          row2.appendChild(td4);
          td1.appendChild(txt1);
          td2.appendChild(newInputFile);
          td3.appendChild(txt2);
          td4.appendChild(newInputTxt);
        }
    this code works fine on FF and opera, but nothing happened in IE7/6.
    also is there anyway to change the name of input everytime( maybe using static var )?

    Thanks in advance.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Add a tbody after the <table> element, then switch the table's id to the tbody and use that instead. That should work.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    What's happening is that when IE parses the HTML code and sees a table, IE automatically inserts the TBODY element that should be there. When a table is given to the page via javascript, IE doesn't automatically check it, so if the TBODY element is missing from the javascript provided table, IE just ignores it completely.

    This is the reasoning behind why table rows should be contained inside a TBODY element, with that TBODY element inside the TABLE element.

    And yes, you can blame IE for this. The specs say that TBODY is optional when there is no header or footer in the table.
    http://www.w3.org/TR/html401/struct/....html#h-11.2.3
    The TBODY start tag is always required except when the table contains only one table body and no table head or foot sections.
    So to keep IE happy, be sure to build your from javascript as follows
    Code HTML4Strict:
    <table>
        <tbody>
            <tr>
                <td>...</td>
            </tr>
        </tbody>
    </table>
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much for your help, I really appreciated.
    one more thing, is there anyway to make those names dynamique? because the user can hit the button more then once, the result is too many input have the same name.

  5. #5
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using PHP to process the form?

  6. #6
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes I m buillding the site with php. is there any probleme?

  7. #7
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay if you're using PHP and name your form fields with "name[]" in PHP you can do:

    PHP Code:
    foreach($_POST['name'] as $image_field)
    {
      
    // Do stuff with each $image_field


  8. #8
    SitePoint Member
    Join Date
    Feb 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. Everything is ok now

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Because you're using PHP you can foto[] across multiple input fields as the name, which PHP receives as an array
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •