Results 1 to 6 of 6

Thread: Multiple Upload Using PHP/Ajax

  1. #1
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Question Multiple Upload Using PHP/Ajax

    Hi

    I am trying to populate new FILE elements on the fly using javascript. I am able to do it but I cannot retrieve the attachment file names when I click on submit. Can you please help me with this?


    Thanks


    Code:
    <html>
    <head>
    
      
      <script>
    
      function addElement() 
      {
      var ni = document.getElementById('myDiv');
      var numi = document.getElementById('theValue');
      var num = (document.getElementById('theValue').value -1)+ 2;
      numi.value = num;
      var newdiv = document.createElement('file');
      var divIdName = 'my'+num+'Div';
      newdiv.setAttribute('id',divIdName);
      newdiv.innerHTML = '<input name="name"   type="file" id=""/>';
      ni.appendChild(newdiv);
    }
      </script>
    </head>
    <body>
      <?php 
      
      if($_POST['submit']=='submit'){
      	foreach($_FILES['name'] as $x){
    		echo $x . '<BR>';
    	}
      }
      
      ?>
    <form method="post" enctype="multipart/form-data">
      <input type="hidden" value="0" id="theValue" />
    <p><a href="javascript:;" onClick="addElement();">Add Some Elements</a></p>
    <div id="myDiv"><input name="name"   type="file" id=""/> </div>
    <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    "file" isn't recognized as an html element. You'll need to use "input" and append a "file" attribute to it.

    Code:
      var newdiv = document.createElement('input');
      newdiv.setAttribute(newdiv, 'file');
    - Josh

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    I didn't read that carefully enough. YOu can't "re-enter" html in an existing html element. Here's a better revised bersion:

    Code:
      function addElement() 
      {
      var ni = document.getElementById('myDiv');
      var numi = document.getElementById('theValue');
      var num = (document.getElementById('theValue').value -1)+ 2;
      numi.value = num;
      var newdiv = document.createElement('input');
      var divIdName = 'my'+num+'Div';
      newdiv.setAttribute('id',divIdName);
      newdiv.setAttribute('type','file');
      newdiv.setAttribute('name','name');
      ni.appendChild(newdiv);
    }
    - Josh

  4. #4
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Change the input name to name[] and see if that works for you.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  5. #5
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    Yes that works


    Thanks

  6. #6
    Join Date
    Dec 2007
    Posts
    123
    Thanks
    17
    Thanked 1 Time in 1 Post

    Default

    Hey

    I was wondering if there is anyway I can add a "Remove" button for each upload field?

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
  •