By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Does AJAX - get data from database and display - work on blog sites?

P: 12
php version - 4.4.4
mysql version - 4.1.22


Hi, my problem is rooted in how blogs handle ajax requests from a database returning html in the form of an image element. So the blog contains reference to a js file on the server which interacts with a php script in the same folder that then returns an image element (eg.
Expand|Select|Wrap|Line Numbers
  1.  <img src="http://www....
) to the js script that writes the code using innerHTML. Page > javascript (creation of XMLHTTPRequest object or IE equivalent) > php script > javascript > Page. The ajax request works fine when I upload a file to my server with the code in the body tag. Something goes wrong when I try pasting the same code in a Blog (that takes pure html). Basically httpRequest.readyState == 4 and httpRequest.status == 200 returns false intstead of true. Can anyone help with this problem??
Nov 2 '07 #1
Share this Question
Share on Google+
13 Replies


acoder
Expert Mod 15k+
P: 16,027
Can you post your code or a link?
Nov 2 '07 #2

P: 12
Here's what the javacript code looks like...
(I'm a newbie at this)
Expand|Select|Wrap|Line Numbers
  1.     function makeRequest(url) {
  2.         var httpRequest;
  3.         if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  4.         httpRequest = new XMLHttpRequest();
  5.                 var browser = "moz";
  6.         if (httpRequest.overrideMimeType) {
  7.             httpRequest.overrideMimeType('text/xml');
  8.  
  9.         }
  10.         } 
  11.         else if (window.ActiveXObject) { // IE
  12.         try {
  13.             httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  14.                     var browser="ie";
  15.             } 
  16.             catch (e) {
  17.                 try {
  18.                     httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  19.                                     var browser="ie";
  20.                 } 
  21.                 catch (e) {}
  22.                 }
  23.                     }
  24.  
  25.         if (!httpRequest) {
  26.         alert('Giving up :( Cannot create an XMLHTTP instance');
  27.         return false;
  28.         }
  29.         //Set function to handle reponse
  30.         httpRequest.onreadystatechange = function() { alertContents(httpRequest,browser); };
  31.         httpRequest.open('GET', url, true);
  32.         httpRequest.send(null);
  33.  
  34.     }
  35.  
  36.     function alertContents(httpRequest,browser) {
  37.         try{    
  38.             if (httpRequest.readyState == 4) {
  39.             if (httpRequest.status == 200) {
  40.                         // Put response text (an img element) in div called 'random'
  41.                         var newdiv = document.createElement("div");
  42.                         newdiv.innerHTML = httpRequest.responseText;
  43.                         var container = document.getElementById("africapic_photos");
  44.                         container.appendChild(newdiv);
  45.                     } else {
  46.                         var newdiv = document.createElement("div");
  47.                         newdiv.innerHTML = "There was a problem with the request.";
  48.                         var container = document.getElementById("africapic_photos");
  49.                         container.appendChild(newdiv);
  50.  
  51.             }
  52.             }
  53.         }
  54.         catch( e ) {
  55.             alert('Caught Exception: ' + e.description);
  56.             }
  57.     }
  58.  
  59. if (document.getElementById('africapic_hl')){
  60. makeRequest('weblinks.php?wli=2997&size=s');}
  61. else {
  62.        var newdiv = document.createElement("div");
  63.     newdiv.innerHTML = "Please use the code provided.";
  64.     var container = document.getElementById("random");
  65.     container.appendChild(newdiv);}
  66.  
The output on a certain blog is "There was a problem with the request"...
Thanks
Nov 2 '07 #3

P: 12
The code above is one of many automatically generated scripts (each passes diff values to weblinks.php. An example of the corresponding html script follows....
Expand|Select|Wrap|Line Numbers
  1. <div id="africapic_photos">
  2.     <a href="http://www.africapic.com/Tranquil-pictures-2997.html" rel="nofollow" id="africapic_hl"> <script src="http://www.africapic.com/wl_js/2997.js" language="javascript"></script></a>
  3. </div>
  4.  
Nov 2 '07 #4

acoder
Expert Mod 15k+
P: 16,027
The output on a certain blog is "There was a problem with the request"...
So obviously the status is not 200. Have you checked what the status is?
Nov 2 '07 #5

P: 12
It was 404. I then changed line 60 to an absolute path and now the code doesn't even reach
Expand|Select|Wrap|Line Numbers
  1. function alertContents(httpRequest) {
  2.         try{    
  3.             alert(httpRequest.readyState);
  4.             if (httpRequest.readyState == 4) {
  5.             alert(httpRequest.status);
  6.             if (httpRequest.status == 200) {
  7.                         // Put response text (an img element) in div called 'random'
  8.                         var newdiv = document.createElement("div");
  9.                         newdiv.innerHTML = httpRequest.responseText; 
  10. ...
  11.  
(there are no alerts). The code works fine on my server but not on a free blog.
Nov 2 '07 #6

P: 12
You've really got me thinking here.
Nov 2 '07 #7

P: 12
Please can anyone help? PLeeease
Nov 3 '07 #8

acoder
Expert Mod 15k+
P: 16,027
Try accessing weblinks.php with the correct arguments without using Ajax. Does it work?
Nov 3 '07 #9

P: 12
Yup that works. I put the image src attribute as the weblinks.php script and altered the php so that it returns an actual image not just text. The reason that I've used ajax is that if you look at post 3 line 59 there's a javascript DOM thing that's vital (it corresponds with the id on post 4 line 2) . Unless there's some way you know of to access the DOM using PHP... even that might still not work...
Nov 3 '07 #10

P: 12
I did the above (post 10) just as a test to see that weblinks.php works fine. In order to send back an image I put a header [php]header("Content-Type: image/jpeg");[/php] Do I need to give a header for outputting text (html)? ie for this (line 6):
[php]
(code to open db connection goes here)
$query="SELECT filename,width,height,category,medianame FROM weblinks WHERE idn=".$_GET['wli']." AND size='".$_GET['size']."'";
$rawdb=mysql_query($query);
$array=mysql_fetch_array($rawdb);
if (!empty($array["filename"])){
echo "<img src='http://www.africapic.com/WebLinks/".$array['filename'].".jpg' width='".$array['width']."' height='".$array['height']."' alt='".$array['medianame']." pictures, ".$array['category']." stock photography' title='".$array['medianame']." pictures, ".$array['category']." stock photography' border='0' />";
}else{
echo "Image not retirievable";
}
mysql_close($dbh);
[/php]
Nov 3 '07 #11

acoder
Expert Mod 15k+
P: 16,027
Do I need to give a header for outputting text (html)?
No. The problem is with not being able to access the file because you're getting a status of 404 Page not found.

Check that the relative path matches. Is it in the same directory?
Nov 3 '07 #12

P: 12
I changed the relative path to an absolute path (post 6) and the 404 disappears. However, I then get nothing (no 404 no 200 not even a readystate). Do blogs by any chance kinda block ajax requests? I'll try the code on another blog quick.
Nov 3 '07 #13

acoder
Expert Mod 15k+
P: 16,027
Do blogs by any chance kinda block ajax requests?
They shouldn't do. You did get a readyState of 4 before, so something's right.

I think the problem is with it not finding the PHP file.
Nov 5 '07 #14

Post your reply

Sign in to post your reply or Sign up for a free account.