473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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
13 2465
acoder
16,027 Expert Mod 8TB
Can you post your code or a link?
Nov 2 '07 #2
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
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
16,027 Expert Mod 8TB
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
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
You've really got me thinking here.
Nov 2 '07 #7
Please can anyone help? PLeeease
Nov 3 '07 #8
acoder
16,027 Expert Mod 8TB
Try accessing weblinks.php with the correct arguments without using Ajax. Does it work?
Nov 3 '07 #9
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
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
16,027 Expert Mod 8TB
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
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
16,027 Expert Mod 8TB
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

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

Similar topics

3
by: Lyle Fairfield | last post by:
In a recent thread there has been discussion about Data Access Pages. It has been suggested that they are not permitted on many or most secure sites. Perhaps, that it is so, although I know of no...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
9
by: schmeckel | last post by:
I am trying to use some very basic AJAX functionality (update panel for partial page update) on my website. When I run my webpage in VS 2005, the partial page update works fine. However, when I...
10
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
controlsPlease could some of you here post some of your live examples of AJAX (esp drag panels, collapsable panels, and popup menu.) (It's one thing to talk about how great something is, but it's...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
3
by: Robert Cramer | last post by:
Just wondering if any of you actually implement portal sites using ASP.NET Web Parts, or use Web Parts outside of portal-style sites. I'm just now learning about Web Parts, and was wondering if...
7
by: raknin | last post by:
I'm using AJAX on my website, but internet explorer does not seem to actually be refreshing the data I retrieve via AJAX when I refresh the page. For example, I have a button that when pressed uses...
8
by: sheldonlg | last post by:
I am still improving my AJAX and I ran into something that I'm not sure how to do. I will have a page called where I will have a dropdown list at the top and a large display at the bottom. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.