tags:

views:

129

answers:

4

hi,

im looking forward to create an upload module where user can browse, click open and it will instantly display a preview of that image without having to click a submit button so that user can continue to key in other information.

i've done a simple but incomplete jquery below which basically capture the image name. but my question is how do i post the upload image to the php script since there is there is no submit button for POST? i cant capture $_FILES array values.

jquery:

$(document).ready(function() {
  $("#uploadimage").change(function() {
      var imagesrc = $("#uploadimage").val();
      $.post("/script/ajax_uploadimage.php", $("#formuploadimage").serialize(),
      function(data){
        //do something here
      },"json");
  });
});

html form:

<form name="formuploadimage" enctype="multipart/form-data" action="/upload.php" method="POST">
    <table> 
        <tr><td>Image: </td><td><div id="imagepreview"></div></td></tr> 
        <tr><td>Upload a photo: </td><td><input type="file" name="uploadimage" id="uploadimage" /></td></tr>
    </table> 
</form>

i've seen what Uploadify can do but i would like to create one on my own.

regards

+1  A: 

You can post image only by posting form, so you must use iframe to upload image without reloading main page. When iframe reloads, add some script in its response which triggers callback function in main page to display just uploaded image.

codez
You could use AJAX. IFrames would have been a pre-AJAX solution.
Lucanos
Tks.i've search quite abit for iframe but most of it needs user to click on the upload button. i would love to see those which can auto upload once user select the file.
CORRECTION: As pointed out by @codez and @bas, files cannot be uploaded through AJAX. My mistake.
Lucanos
hi lucanos, so wat is the way around it? can i use ajax submit to post $_FILES to php files??
@nuttynibbles: Look at the tutorial I linked you to, it should have all the code, links, and instructions you need to achieve the same kind of functionality you require.
Lucanos
tks i managed to solve it using iframe with YUI lib =) so basically this YUI library detects onchange event and post to the php script which would handle upload request =)
+1  A: 

Files cannot be uploaded using pure AJAX. You may want to checkout the Form Plugin which does support file uploads: http://jquery.malsup.com/form/ and integrate it into your solution. There you have few good examples using ajaxSubmit.

bas
+1  A: 

I saw an article with a jQuery solution for this recently:

Zurb Playground : "Image Uploads with 100% Less Suck. Guaranteed."

I would rewrite it here, but have not as it would be redundant.

Lucanos
hey this is what im looking for. but there isn't any file download for it. but i will try to piece the codes together from view source =)
1st. Its not a jquery solution. 2nd. Its not even ajax solution. If you look carefully to js source files you will discover that this is iframe solution.
codez
@codez - You are both right and wrong. I never said it was an AJAX solution, so that point is moot. Yes, the AJAX Upload function does use an iframe to perform the work, but, that function has been wrapped into a jQuery plugin. So, in essence, it is a jQuery solution.
Lucanos
hmm okay. from the solution, i dun see where is the php script call to upload the image.
okay i think i know where to call the php script.
@Lucanos - In comment on my answer you said that it can be done with AJAX, so I think that you thought that it is ajax solution, in fact there is no way you can send file via ajax in standart browser with no special browser plugins or configuration. And Ajaxupload in that example is not wrapped in jquery plugin, look how it is called in sample: new AjaxUpload(). jquery plugins are called through $ object. If it were jquery plugin, it would be called like: $('.some').AjaxUpload({...});
codez
hi can i know how i can post $_FILES value into php script?? im able to call upload.php but the $_FILES value is empty
@codez: I am more than happy to concede the point, the comment regarding AJAX was in reply to your solution (hence I lost track of it until just then) and was before I had researched the idea further. Agreed AJAX cannot handle files. Again, this became obvious as I looked further. The example I posted a link for was posted "from-the-hip" I didn't look further to see how the supporting code worked, I just saw the article saying that it did and referring to jQuery. Regardless - it works, and that's what matters, not the minor details of a brief response.
Lucanos
@codez: (follow-on) If you want to discuss the inaccurate use of "AJAX" in the article I linked to, it may better be addressed to the author on Zurb.com. I have posted a comment under your solution correcting my incorrect comment regarding use of AJAX to handle the file upload.
Lucanos
A: 

You might like to try this tutorial:

http://www.finalwebsites.com/forums/topic/php-ajax-upload-example

which should help you do exactly what you are asking.

Craig