Multiple Upload Using PHP/Ajax

  • cancer10
  • Proficient
  • Proficient
  • cancer10
  • Posts: 268

Post 3+ Months Ago

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: [ Select ]
<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>
  1. <html>
  2. <head>
  3.  
  4.  <script>
  5.  function addElement()
  6.  {
  7.  var ni = document.getElementById('myDiv');
  8.  var numi = document.getElementById('theValue');
  9.  var num = (document.getElementById('theValue').value -1)+ 2;
  10.  numi.value = num;
  11.  var newdiv = document.createElement('file');
  12.  var divIdName = 'my'+num+'Div';
  13.  newdiv.setAttribute('id',divIdName);
  14.  newdiv.innerHTML = '<input name="name"  type="file" id=""/>';
  15.  ni.appendChild(newdiv);
  16. }
  17.  </script>
  18. </head>
  19. <body>
  20.  <?php
  21.  
  22.  if($_POST['submit']=='submit'){
  23.      foreach($_FILES['name'] as $x){
  24.         echo $x . '<BR>';
  25.     }
  26.  }
  27.  
  28.  ?>
  29. <form method="post" enctype="multipart/form-data">
  30.  <input type="hidden" value="0" id="theValue" />
  31. <p><a href="javascript:;" onClick="addElement();">Add Some Elements</a></p>
  32. <div id="myDiv"><input name="name"  type="file" id=""/> </div>
  33. <input type="submit" name="submit" value="submit">
  34. </form>
  35. </body>
  36. </html>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

What you have seems pretty legit. Are you saying you're not getting output? If so, try this to make sure:

PHP Code: [ Select ]
<?php
if($_POST['submit']=='submit'){
echo print_r($_FILES,TRUE);
}
?>
  1. <?php
  2. if($_POST['submit']=='submit'){
  3. echo print_r($_FILES,TRUE);
  4. }
  5. ?>


I don't think I'm missing something, however I might be. If that doesn't work, maybe it's a PHP configuration problem? I don't exactly have your script in front of me to work error messages, so I may be wrong.

If you're trying to get the file as an attachment, you need to do quite a bit more work than that. What happens is it's put into the servers temp directory and you need to do a bunch of security checks, once you have the data then you can spit it back out as an attachment with a couple HTTP headers.
  • cancer10
  • Proficient
  • Proficient
  • cancer10
  • Posts: 268

Post 3+ Months Ago

Not sure what you are saying. I got that code from the internet soemwhere and I cant get to run it. I am new to ajax:(


please help

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 159 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.