Hello, I'm trying to pull an image in a function instead of text with javascript. Here's how it looks with text. function handleResponse() { if(http.readyState == 4 && http.status == 200){ // Text returned FROM PHP script var response = http.responseText; if(response) { document.getElementById("countPosts").innerHTML = response; setTimeout('countPosts()',2000); } I imagine I need this added document.getElementById("countPosts").src = But I how do I adjust the rest? Could really use some help on how to do it with images instead. Please let me know, thank you very much.
We need to see the whole code to write a solution for that one, it depends on wether countPosts is an <img> tag or another html tag.
Your right hadn't thought of that. I suppose if I am pulling an image from another page it needs to go into an image id then. I'm not aware you can do it the other way. That would be fine. The http request is pulling from another page called test.php. And putting what in it in an id called "countposts." I'm trying to pull an image from that php page. So basically I'm just trying to call the image in the page, not any text at all. So I think I can get rid of the text commands in the javascript and do something like this instead. But I think there is something missing in the getelement by id, like a src. command or something? Anyone know how to take an image from a php page and stick it in an id? I'm open to all suggestions. You don't need to give me any http request code, or body code, I've got all that. This is the only part I'm having trouble with. function handleResponse() { function handleResponse() { if(http.readyState == 4 && http.status == 200){ document.getElementById("countPosts") setTimeout('countPosts()',5000); } } } function countPosts() { sendRequest('countPosts'); } Here's the php thats in the php page with the image I'm trying to pull. <?php $str = '$getfile = date("s"); $imagefile = "file" . $getfile . ".jpg"; echo "<img src=\'/$imagefile\'>";'; eval($str); ?>
Whatever your PHP script outputs, it is still called responseText regardless of whether it's plain text or in this case HTML. If you already have the image element and want to change the image location, you would have the PHP script output just the URL to the image and use: document.getElementById('myimage').src = http.responseText; If you simply have a container element, and your PHP script outputs the whole <img> tag (as would appear to be the case), you'd use: document.getElementById('mydiv').innerHTML = http.responseText;
Thanks very much I appreciate that knowledge, thats good to know. I'll play around with it and see what happens.