HTML fo uploading image file
-
Hello i'm trying to make a form to upload an image. So i combined two events in one just to click only once an upload the file. But when i click and choose the file, the tmp file is not uploaded. here's my code, do you have an idea of why? Thank you in advance.
-
Hello i'm trying to make a form to upload an image. So i combined two events in one just to click only once an upload the file. But when i click and choose the file, the tmp file is not uploaded. here's my code, do you have an idea of why? Thank you in advance.
Well, a lot of it has to do with the fact that you're applying a synchronous methodology to an asynchronous construct. Event handlers do not block the main thread, they run in their own context. That means that the form is being submit before any file is actually chosen.The better route is to use an event handler.
$('#file').change(function(){
$('#theForm').submit();
});Then your HTML could look something like:
<form id="theForm" target='_self' action='upload.php' method='post' enctype='multipart/form-data'>
<input style='display:none' type='file' accept='.jpg' name='file' id='file'/>
<input type='hidden' name='id_utente' value='".$user['id']."' />
<input type='button' value='Scegli file da PC' onClick="$('#file').trigger('click');" />
</form>"There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli
-
Well, a lot of it has to do with the fact that you're applying a synchronous methodology to an asynchronous construct. Event handlers do not block the main thread, they run in their own context. That means that the form is being submit before any file is actually chosen.The better route is to use an event handler.
$('#file').change(function(){
$('#theForm').submit();
});Then your HTML could look something like:
<form id="theForm" target='_self' action='upload.php' method='post' enctype='multipart/form-data'>
<input style='display:none' type='file' accept='.jpg' name='file' id='file'/>
<input type='hidden' name='id_utente' value='".$user['id']."' />
<input type='button' value='Scegli file da PC' onClick="$('#file').trigger('click');" />
</form>"There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli
HI, ok i understand the 2 asynchronous construct, but the code you wrote is not working. Maybe that i need to raise first event to choose the file and then the second event (the button "upload image") is raised still before the load of image. the change event is not working, it seems. Thank you