views:

74

answers:

3

Hello,

I kept one jquery uploadify plugin on page and one <img> tag besides it. Everything works fine as expected except one problem. I want to update <img> to show the new image as soon as some image is uploaded. How can i do this? I am using Php.

Thanks in advance :)

+1  A: 

You can use onComplete callback (http://www.uploadify.com/documentation/).

Usage sample is here:

$(document).ready(function() {
  $('#fileInput').uploadify({
    'uploader'  : 'uploadify.swf',
    'script'    : 'uploadify.php',
    'cancelImg' : 'cancel.png',
    'auto'      : true,
    'folder'    : '/uploads'

    /* ====== HERE ===== */
    'onComplete': function(event, queueID, fileObj, response, data) {       
        $("#my_image").attr("src","your path");
     }

  });
});

(http://www.uploadify.com/implementation/)

MartyIX
Hi MartyIX, I am actually using echoing some strings like `photo uploaded successfully` etc. How will i get the image path? I am using uniqid to generate unique image names for my images. How can i use those image names? in `your path`?
Ankit Rathod
You can echo the path of the image in your php file and then read it from parameter `response`.
MartyIX
If you want to get from php more data than just the url of image you can use JSON for communication between php and javascript. Otherwise you will have to parse the data from php by hand.
MartyIX
Hi MartyIX, that's what i am thinking how can i do that? since i am doing 2 echos `echo 'Photo uploaded successfully'; echo 'imagePath';`. Now the `response` will contain both the strings. How do i then know which is the image path and which isn't?
Ankit Rathod
Ok, can you point it out it to me, how can i use JSON in php with jquery?
Ankit Rathod
In your case I would simply do: echo 'Photo uploaded successfully;imagepath' and on javascript side I would split the string by semicolon delimiter. http://www.w3schools.com/jsref/jsref_split.asp The choice of delimiter is up to you, though ;-)
MartyIX
A: 

Just use JQuery to update the <img src="..." /> attribute of that image should do it IMHO.

Rosdi
A: 

Take a look at the documentation, it looks like the onComplete callback will return the file path, you could then do something like this:

onComplete: function(event, queueID, fileObj, response, data) {
    $('img#user_avatar').attr('src',fileObj.filePath);
}
ILMV
what is `fileObj.filePath`? how will the path be set in fileObj.filePath? I am using `$_FILES` to save images at my desired location and after renaming the picture name. How can `fileObj.filePath` know what is my new image name and it's location?
Ankit Rathod
I was just working off of the documentation, if you're returning a value from your PHP script you could return the path of the image then apply that instead.
ILMV