PHP & JQUERY + how to let Jquery know when a file upload has completed

I have a script working to upload images without refreshing the page using jquery.form.js

Below is the PHP file that it kicks off the processes the file and when finished PHP creates an tag to show the image.

I now need a method to let JQUERY know the PHP file processing has completed. Is there a good way to connect these two?

I thought I could write something hidden to the page and have JQUERY look for this but I'm not sure if this is a B-Grade solution.

Any ideas? I can explain better if needed. thx


    $type = $_POST['mimetype']; 
  $xhr = $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; 

    foreach($_FILES as $file) { 
            $filename = $file['name']; 
            $filesize = $file['size'];
            $filetmp = $file['tmp_name'];

    // Script Variables
    $maxsize='10485760';                                    // 10MB Maximum Upload
    $folder = 'images/';
    $configWidth = 500;
    $newFileName = 'success_story'.time().'.jpg';

    // Process if No Errors
    if(!$errors) {

        // Variables
        $uploadedFile = $filetmp;
        $filename = basename( $filename);
        $extension = strtolower(getExtension($filename));

        // Convert the Specific Type of File into an Image
        if($extension=='jpg' || $extension=='jpeg' ) {
            $uploadedfile = $fullfilepath;
            $src = imagecreatefromjpeg($uploadedFile);
        }elseif($extension=='png') {
            $uploadedfile = $fullfilepath;
            $src = imagecreatefrompng($uploadedFile);
            $uploadedfile = $fullfilepath;
            $src = imagecreatefromgif($uploadedFile);

        // Configure Width & Height
        list($origWidth, $origHeight) = getimagesize($uploadedFile);
        $configHeight = ($origHeight/$origWidth)* $configWidth;

        // Create Empty File
        $tmp = imagecreatetruecolor($configWidth, $configHeight);
        imagecopyresampled($tmp, $src, 0,0,0,0,$configWidth,$configHeight,$origWidth,$origHeight);
        imagejpeg($tmp, $_SERVER['DOCUMENT_ROOT'].$folder.$newFileName,90);

        echo "<img src=".$folder.$newFileName." id=\"cropMeNowImage\">";

    // Get Extension from Uploaded File
    function getExtension($str) {
        $i = strrpos($str,".");
        if (!$i) {return "";}
        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
        return $ext;

<script src=""></script>
(function() {

    $(document).on('change','img#cropMeNowImage', function() {
        alert('Ready to Crop');



You need to use a callback function. Something like:

$(document).on('change','img#cropMeNowImage', function() {
    $.post(url, {
        vars_to_server : vars
    }, function(process_done){
        if (process_done)


php must echo something recognizable by jquery in the var process_done


echo "<img src=".$folder.$newFileName." id=\"cropMeNowImage\">";

you can echo 1 for success

This is the idea. It's totally possible. Specially for the common task of upload files via AJAX...

I recommend you: 5 Ways to Make Ajax Calls with jQuery

Take a look to I always use it to upload files.

Need Your Help

Pass value to controller from link name Rails


I have a list of names, that contain this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.