0

I have this image upload html code with a button says'upload', so, these both 'input' and 'submit' button are inside an form. so, when user clicks on input, it asks for a image to select, once selected, user needs to click on upload button to submit the image to the form url.

Now i m trying to submit the image to form url without the involvement of upload button. i.e., when user clicks in input field to select an image, it should submit automatically.

<form id="uploadform" target="upiframe" action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="fileToUpload" class="test" onchange="yourFunction()">
</form>

<iframe id="upiframe" name="upiframe" witdh="0px" height="0px" border="0" style="width:0; height:0; border:none;"></iframe>

<script>
function yourFunction() {
    var form = document.getElementById('uploadform');
    form.submit();
  // }); remove this
}
</script>

Any Suggestion is Appreciated..

2 Answers 2

1

This will work you need to use pure Javascript method

document.getElementById("uploadform").submit();

function yourFunction(){
  document.getElementById("uploadform").submit();// Form submission
}
<form id="uploadform" target="upiframe" action="upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="fileToUpload" class="test" onchange="yourFunction()">
</form>

<iframe id="upiframe" name="upiframe" witdh="0px" height="0px" border="0" style="width:0; height:0; border:none;"></iframe>

0

Could you use jQuery to listen for change events on the input:

 $('#file').on('change', function() { $('#form').submit(); });
5
  • i just tried with JS.. please check the update on question.. seems like it is also not working..
    – harishk
    Commented Apr 21, 2017 at 6:09
  • There's a syntax error. Was that there when you tried?
    – coopwatts
    Commented Apr 21, 2017 at 6:20
  • $('#file').on('change', function() { $('#uploadform').submit(); }); Commented Apr 21, 2017 at 6:22
  • }); underneath form.submit()
    – coopwatts
    Commented Apr 21, 2017 at 6:24
  • @coopwatts can you correct it. i have no idea how to do it?
    – harishk
    Commented Apr 21, 2017 at 6:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.