Luke
Luke

Reputation: 5971

How do i load an image with ajax/jquery?

I need to load an image, that is generated by a PHP script. The PHP script accepts a three dimensional array as a parameter and creates from it an image. How do i pass the arguments with ajax? How do i load the image?

Upvotes: 1

Views: 385

Answers (3)

tonycoupland
tonycoupland

Reputation: 4247

I have done something similar but using ASPX rather than PHP, I used query string params to generate the image, and jQuery to load the image into an existing object in the document:

var chartRootURL = 'http://127.0.0.1/';
var chartQueryParameters = 'inst=APPL&w=900&h=200';
var url = chartRootURL + 'chartImage.aspx?' + chartQueryParameters +
                    '&ts=' + new Date().getMilliseconds();

var img = $('#chartimg').attr('src', url)
                    .load(function (response, status) {
                        $('#chartimg').show();
                    });

Upvotes: 2

ilivewithian
ilivewithian

Reputation: 19712

How about

document.write("<img src='http://www.aurl.com/generator.php?" + yourParams + "' alt='exciting image here' />")

Upvotes: -2

Michael Lorton
Michael Lorton

Reputation: 44436

There's no way (barring some really hairy <canvas> tag magic) to move data from Javascript into an image, but fortunately, you don't have to. All you have to do generate the URL for the PHP script and assign it to an Image and boom.

var img = new Image();
var img.src = "http://phphost.net/imagegenerate.php?array=3,1,2,3,21,1"; // or whatever

Upvotes: 2

Related Questions