2 Replies - 3750 Views - Last Post: 22 December 2012 - 02:31 AM

#1 geohhot  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 22-December 12

How to make form submit when file is selected in input

Posted 22 December 2012 - 01:24 AM

My question is: if I have for example form and input, type="file" inside it:
<form action="upload.php" method="POST">
    <input type="file" name="file" />
</form>


How to make form submit when file is selected, without clicking submit.
Nathan told me, that it is impossible, but for example when uploading new cover on facebook, it works.
What I must write for javascript code?

Is This A Good Question/Topic? 1
  • +

Replies To: How to make form submit when file is selected in input

#2 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: How to make form submit when file is selected in input

Posted 22 December 2012 - 01:42 AM

Well, Nathan is wrong. When the value of an <input> is changed, the "change" event is fired. All you have to do is listen for that event and then trigger the submit() function on the form.

These pages should get you started on that.
- element.onchange
- form.submit

If you are new to Javascript and using the DOM, you may need to read this one too:
- document.getElementById

Note that the "onclick" handling described in the link above is the "old-school" version of event handling. More modern code uses the addEventListener function for event handling. It's a tad less simple, but if you are planing on using Javascript much, you should definitely learn that too.
Was This Post Helpful? 1
  • +
  • -

#3 geohhot  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 33
  • Joined: 22-December 12

Re: How to make form submit when file is selected in input

Posted 22 December 2012 - 02:31 AM

thanks.. worked with addEventListener.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1