File Upload / Javascript Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fangorn
Forum Commoner
Posts: 41
Joined: Fri May 21, 2004 9:04 am

File Upload / Javascript Question

Post by fangorn »

I'm trying to restrict my users to only be able to upload PDF files.
This code below works for a single file.

Code: Select all

<script>
  function valid_form() &#123;
    var temp = document.sc_files.sd_file.value;

    if (temp.length > 0) &#123;
      if (temp.indexOf(".pdf") == -1) &#123;
        alert("Support docs must be PDF files.");
        return false;
      &#125;
    &#125;
  return true;
  &#125;
But I am now offering multiple files at once instead of just one and I used the PHP feature of have these files uploaded in an array.

Code: Select all

<tr><td><input type=file size=40 name=sd_files&#1111;]></td></tr>
<tr><td><input type=file size=40 name=sd_files&#1111;]></td></tr>

This works just fine for uploading the files. But I want to modify my JS validation to work with the multiple files. When I try to reference in JS:

Code: Select all

var temp = document.sc_files.sd_files&#1111;0].value;
This is undefined. Does anyone know which variable name in JS can be used with the array style input naming convention?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

document.forms&#1111;formname].elements&#1111;index]
fangorn
Forum Commoner
Posts: 41
Joined: Fri May 21, 2004 9:04 am

Post by fangorn »

aha, thanks alot!
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

test

Post by phpScott »

make sure you test that the element type is of file or you could get some odd things happening.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Don't forget to validate serverside as well :)

Mac
Post Reply