Using js to click type=file

Discussion in 'JavaScript' started by Snarf, Jun 27, 2006.

  1. #1
    Hi, I'm trying to make a page with HTML, PHP and JS wich should realise a file upload. The code i'm using is shown below :

    <code>
    <script type="text/javascript">
    function getfile(){
    document.getElementById('hiddenfile').click();
    document.getElementById('selectedfile').value=document.getElementById('hiddenfile').value
    }
    </script>

    <FORM ACTION="<?php echo(BASENAME($_SERVER['PHP_SELF']))?>" METHOD=POST enctype=multipart/form-data NAME=general>
    <TR><TD>
    <input type="file" id="hiddenfile" name="hiddenfile" value="<?echo $_POST['hiddenfile'];?>"/>
    <input type="text" id="selectedfile" value="<?echo $_POST['selectedfile'];?>"/>
    <input type="button" value="Select a file" onclick="getfile()" />
    <input type="submit" value="Verstuur"/>
    </TR</TD>
    </code>

    The problem is that the submit clears the field with name="hiddenfile" on the moment (before, i believe) the upload must be realised, so there is no upload no filled $_FILES. Who can tell me what i'm doing wrong ?

    Greets Frans
     
    Snarf, Jun 27, 2006 IP
  2. pl4y3r

    pl4y3r Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    wouldn't it be simplier to use a form without JS? Only a FILE input and a submit button.

    Javascript is very tricky if you don't know it well (happens to me all the time :p)
     
    pl4y3r, Jun 27, 2006 IP
  3. Snarf

    Snarf Banned

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Yes that is much simplier, it's what I had before. I want to change direct some things with js.
     
    Snarf, Jun 28, 2006 IP
  4. Snarf

    Snarf Banned

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    I found a solution for this problem, i inserted a "onchange" between the tags of the type=file field. Now I am able to display directly the chosen image !
     
    Snarf, Jun 28, 2006 IP