SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    populating a form field

    Hello, I'm new to this forum but have a question:

    Can I populate a file form field (<input type="file">) with javascript? If so how?

    What I need to do is to upload a file to the server without user intervention. I'm getting the local path to the file as a post variable and want to be able to upload it directly without showing the user a "browse" button. I thought I could use javascript to populate a <input type="file"> form field and then submit that automatically.

    Is this possible? Is there a better/easier way?

    Thanks
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Javascript can't read POST variables. So, your choices are:

    1) Change the previous submission to GET (if you do this, I can help with the rest)
    2) Use a server-side scripting language that CAN read POST data (PHP, ASP, Java, Perl, etc...)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Beetle I think I can do GET instead of a POST, but if there is any way to do what I want to do without javascript and just strainght PHP I'm in, I just didn't think there was because all I've seen about uploading files is with an HTML form. In fact I'd rather do it all in PHP!
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I code PHP, but I have yet to mess with file uploading. Post about this in the PHP forum, I'm sure it can be done. If not...come back here and I'll show you how to extract GET variables effectively with javascript.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if you get a chance to show me how to do this with javascript (I already posted it on the PHP forums, no hits yet except page admins about a different issue) I'd really appreciate it.

    I've got 2 really good books on PHP (O'reilly Web Database Applications, excellent and WROX Beginning PHP) both of them mention that to upload you need to use an HTML form. Also everything else I've read seems to suggest the same.

    Thanks
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  6. #6
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm confused.

    What is wrong with either:

    PHP Code:
    <input type="text" name="filename" value="<?=_POST["variable_name"];?>">
    or applying the same idea to the input type=file? I have never tried it, but I assume it'd work.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  7. #7
    SitePoint Evangelist
    Join Date
    May 2002
    Location
    Barcelona, Spain
    Posts
    434
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re: confused

    what's wrong is that that doesn't work (try it). You cannot put a value into a file type field (at least I can't) no matter how hard I try. Even with javascript (somebody sent me the right code).

    The file field refuses to take an input other than an interactive browse. Even if you put just a string as the value and forget about any variables.

    Arrrghhhh.

    There has to be a way

    How about this? I should be able to do it if I sent the right information through HTTP headers no? Does anyone know how to do this?

    Thanks again
    Last edited by pata; Oct 18, 2002 at 12:08.
    Used to prefer PHP.
    Now I prefer Maya but stuck with PHP
    bruji.com

  8. #8
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes. For security purposes, the value property on input type=file is read-only. Sorry, unless you can find a PHP solution, I don't think you are going to be able to do this.

    Read more:
    http://msdn.microsoft.com/workshop/a...es/value_6.asp
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  9. #9
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, like I said, I didn't know if you could do it. What is wrong with using a regular text input field?
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  10. #10
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because it won't pull a file from the client machine and send it. It will just send the path as text.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  11. #11
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by beetle
    Because it won't pull a file from the client machine and send it. It will just send the path as text.
    D'6h, yeh. Sorry was very tired last night.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  12. #12
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: populating a form field

    Originally posted by pata
    Can I populate a file form field (<input type="file">) with javascript? If so how?
    This is most definitely not possible because of the horrific security implications that would ensue if it were!(think about it)

    It is possible that you might be able to find a Java or ActiveX file upload component that might let you do this, but I think it unlikely for the same reasons.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!


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
  •