1

The following form allows an user to upload an image:

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

I'd like this form to be submitted as soon as the user selectes its file, so in javascript I did

$(document).ready(function(){
    $("form").on('submit',function(e){
        console.log("It works");
    });
});

function submitForm()
{
    $('form').submit();
}

Which works!

I know want my user to stay on the current page once he the form is being submitted, I tried to add a call back function as soon as the form is submitted:

function submitForm()
{
    $('form').submit(function(e){
         e.preventDefault();
    });
}

But then my form is not being submitted...

I also would like to know how to get the result of the ajax call?

woshitom
  • 4,811
  • 8
  • 38
  • 62
  • If you want the for to be submit and have the user stay on the current page, then you will need to use AJAX. There are lots of questions and tutorials about this already if you research. – Rory McCrossan Jan 31 '18 at 09:24

0 Answers0