0

I've uploaded some photos via ajax. then the response is the json encoded image names. I want to view images imminently after upload proccess.I've tried this:

JS:EDITED

$('#upload-form').ajaxForm({
        success: function(data) {
            $(".loader").hide();
            $("#status").html("");
            $.each(data, function(index, item) {
                $("#status").append("<img src='<?php echo base_url()."assets/img/"; ?>" + item.name +"' />");
            });
            return false;
        },
        complete: function(xhr) {
            $(".loader").hide();
            return false;
        },
        error : function(xhr) {
                $("#status").html(xhr.responseText);
                $(".loader").hide();
        }
    });

but still no luck. How can I do this?

9
  • 1
    Just a nitpick, you should name the first parameter in your success something other than xhr, it isn't an xhr object. Commented Nov 15, 2013 at 18:18
  • Why is there a PHP tag in your JavaScript? Commented Nov 15, 2013 at 18:18
  • 1
    @MattBall, It works matt.it gives me the full url. Commented Nov 15, 2013 at 18:19
  • 1
    @1linecode This is the documentation for the XHR object. developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest Commented Nov 15, 2013 at 18:21
  • 1
    jQuery has no $("#status").appendChild method, it's $("#status").append Commented Nov 15, 2013 at 18:22

1 Answer 1

1

trying to fix the code...

I assume ajaxForm is some sort of plugin, and xhr works as you think it does

No need for two loops, and no need for an intermediate array, you can create image nodes directly.

var baseUrl = "<?php echo base_url(); ?>" + "assets/img/"; 

$('#upload-form').ajaxForm({
    success: function(xhr) {
        $(".loader").hide();
        $("#status").html("");
        var images = "";
        $.each(xhr, function(index, item) {
            images += "<img src='"+ baseUrl + item.name + "' />";
        });
        $("#status").append(images);
        return false;
    },
    complete: function(xhr) {
        $(".loader").hide();
        return false;
    },
    error : function(xhr) {
            $("#status").html(xhr.responseText);
            $(".loader").hide();
    }
}); 
Sign up to request clarification or add additional context in comments.

1 Comment

just made a change... try again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.