Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 8
There are 1 users currently browsing forums.
PHP Scripting PHP is a scripting language for rapid web-development. It's a popular and powerful, OOP-capable language that is used by professionals. Discuss PHP here.

Reply
  #1  
Old 09-18-2009
cancer10's Avatar
A "Mature" Pre-Teenager!
 
Join Date: Jun 2005
Location: India
Age: 28
Posts: 517
Rep Power: 6
cancer10 is on a distinguished road
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>
Reply With Quote
  #2  
Old 09-18-2009
Toddler
 
Join Date: Nov 2007
Posts: 29
Rep Power: 0
juddster is on a distinguished road
Re: Multiple Upload Using PHP/Ajax

Hi,

I went over your code and was able to fix it relatively easily. All you need to do, is tell HTML that each of the name elements are an 'array'.

To do this you do the following:

Change this:

Code:
<input name="name"   type="file" id=""/>
To this:

Code:
<input name="name[]"   type="file" id=""/>
For both spots where it is located in the script (in the JavaScript portion and in the HTML portion).

This will tell PHP to treat it differently...you can see what it does by using print_r ( $_FILES ); to echo out what is being stored there.

For the full script, see below.

Hope this helps,
~juddster

PHP 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>';
    }*/
print_r $_FILES );
  }
  
  
?>
<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>
Reply With Quote
  #3  
Old 09-18-2009
cancer10's Avatar
A "Mature" Pre-Teenager!
 
Join Date: Jun 2005
Location: India
Age: 28
Posts: 517
Rep Power: 6
cancer10 is on a distinguished road
Re: Multiple Upload Using PHP/Ajax

That works like a charm!


Thanks
Reply With Quote
  #4  
Old 09-19-2009
Toddler
 
Join Date: Nov 2007
Posts: 29
Rep Power: 0
juddster is on a distinguished road
Re: Multiple Upload Using PHP/Ajax

Quote:
cancer10 originally posted: View Post
That works like a charm!


Thanks
No problem. I'm glad I could help out

~juddster
Reply With Quote
  #5  
Old 10-15-2009
cancer10's Avatar
A "Mature" Pre-Teenager!
 
Join Date: Jun 2005
Location: India
Age: 28
Posts: 517
Rep Power: 6
cancer10 is on a distinguished road
Re: Multiple Upload Using PHP/Ajax

Quote:
juddster originally posted: View Post
No problem. I'm glad I could help out

~juddster
Hey

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


THanks
Reply With Quote
  #6  
Old 03-11-2010
bluepicaso's Avatar
Web Developer/ Programmer
 
Join Date: Mar 2010
Location: delhi, India
Age: 23
Posts: 13
Rep Power: 0
bluepicaso is on a distinguished road
Re: Multiple Upload Using PHP/Ajax

Try Using UPLOADIFY
Reply With Quote
  #7  
Old 03-11-2010
bluepicaso's Avatar
Web Developer/ Programmer
 
Join Date: Mar 2010
Location: delhi, India
Age: 23
Posts: 13
Rep Power: 0
bluepicaso is on a distinguished road
Re: Multiple Upload Using PHP/Ajax

Try Using Uploadify
Reply With Quote
  #8  
Old 03-12-2010
Looking#'s Avatar
Toddler
 
Join Date: Mar 2010
Location: Maryland
Posts: 24
Rep Power: 0
Looking# is on a distinguished road
Re: Multiple Upload Using PHP/Ajax

If you're working with uploading/removing files, I suggest that you switch over to PHP. Just my opinion.
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Image Upload problem koolman PHP Scripting 5 11-15-2008 04:38 AM
Multiple Inheritance or Mixins or something _jameshales PHP Scripting 4 11-27-2005 03:32 AM
How to Upload files in ASP .NET Spy^ General Web Programming 0 01-02-2005 09:46 AM
How to Upload files in ASP .NET Spy^ General Web Programming 0 01-02-2005 09:39 AM
PHP Upload Vio-Bear PHP Articles 0 09-04-2004 05:36 AM