Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. HTML fo uploading image file

HTML fo uploading image file

Scheduled Pinned Locked Moved Web Development
htmlquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FilippoCSM
    wrote on last edited by
    #1

    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.

    N 1 Reply Last reply
    0
    • F FilippoCSM

      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.

      N Offline
      N Offline
      Nathan Minier
      wrote on last edited by
      #2

      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

      F 1 Reply Last reply
      0
      • N Nathan Minier

        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

        F Offline
        F Offline
        FilippoCSM
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups