javascriptfileformssubmit

How do I submit a "file" input without submit button using JavaScript?


There is a way to automatically submit a form without clicking to a "submit" button?

I have a form with one "file" input. I would submit the form after the user have selected one file.


Solution

  • yes, you can use the form.submit() function. Add an onchange listener on the file input and link it to the form.submit() function, like so:

    <form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" onchange="this.form.submit()" name="myFile"/>
    </form>