473,748 Members | 9,913 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP File not loading via AJAX

nathj
938 Recognized Expert Contributor
Hi,

I am currently working on a new site that offers various files for download. The file information is stored in a MySQL database and the page is produced in PHP depending on how the user got to the page:

[php]
<?php
session_start() ;
$_SESSION['pageStyle'] = $_SESSION['pageStyle']; // set to whatever is, as you can only access this page from within a top level section
?>
<!--
File: download.php
Author: Nathan Davies
Created: 09/08/2007
Purpose: The downloads from the CLF site - for each topic type
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<?php
include("includ e/pageHeader.php" );

if($loUser->hasAccess(1) ||$loUser->hasAccess(2) || $loUser->hasAccess(3) )
{
// establish where this page was called from
if(isset($_GET['topicID']) && isset($_GET['topicType'])) // use isset as 0 is valid for this page
{
$lnTopicID = $_GET['topicID'] ;
$lnTopicType = $_GET['topicType'] ;

switch($lnTopic Type)
{
case 0: // ad hoc entries that appear under general resources
$lnSubSection = 4 ;
$lcDownloadSele ct =
"SELECT
a.ID, a.name, a.url, a.typeID,
b.description
FROM download a
LEFT OUTER JOIN downloadtype b on b.ID = a.typeID
WHERE a.topicType = $lnTopicType
ORDER BY a.typeID ASC" ; // this set is only ever the full set
break ;
case 1: // event downloads
$lnSubSection = 2 ;
$lcDownloadSele ct =
"SELECT
a.ID, a.name, a.url, a.typeID, a.topicID,
b.title,
c.description
FROM download a
LEFT OUTER JOIN event b on b.ID = a.topicID
LEFT OUTER JOIN downloadtype c on c.ID = a.typeID
WHERE a.topicType = $lnTopicType" ;

// if only one specific event then add that condition to the SQL statement
if($lnTopicID > 0)
{
$lcDownloadSele ct .= " AND a.topicID = $lnTopicID" ;
}
$lcDownloadSele ct .= " ORDER BY a.topicID ASC" ;
break ;
case 2: // module downloads
$lnSubSection = 3 ;
$lcDownloadSele ct =
"SELECT
a.ID, a.name, a.url, a.typeID, a.topicID,
b.title,
c.description
FROM download a
LEFT OUTER JOIN module b on b.ID = a.topicID
LEFT OUTER JOIN downloadtype c on c.ID = a.typeID
WHERE a.topicType = $lnTopicType" ;

// if only one specific module then add that condition to the SQL statement
if($lnTopicID > 0)
{
$lcDownloadSele ct .= " AND a.topicID = $lnTopicID" ;
}
$lcDownloadSele ct .= " ORDER BY a.topicID ASC" ;
break ;
}

include("includ e/subSectionTitle AndNavigation.p hp") ;

// execute the query

$laDownload = $loDB->queryGetData($ lcDownloadSelec t) ;

if($laDownload)
{
// there is information to display
switch($lnTopic Type)
{
case 0: // general
echo '<h1 class="pageTitl e">General Downloads</h1><br />' ;
echo $lcGeneralText ;
echo '<p>' ;
echo 'The items available here are listed by file type - e.g. PDF, MP3 etc' ;
echo '</p>' ;
break ;
case 1: // event
echo '<h1 class="pageTitl e">Event Downloads</h1><br />' ;
echo $lcGeneralText ;
echo '<p>' ;
echo 'The items available here are listed by event title' ;
echo '</p>' ;
break ;
case 2: // module
echo '<h1 class="pageTitl e">Training Downloads</h1><br />' ;
echo $lcGeneralText ;
echo '<p>' ;
echo 'The items available here are listed by module title.';
echo '</p>' ;
break ;
}

$lnCurrentCateg oryID = -1 ;
foreach($laDown load as $laDownloadItem )
{
switch($lnTopic Type)
{
case 0: // general
$lnNextCategory ID = $laDownloadItem['typeID'] ;
if($lnNextCateg oryID != $lnCurrentCateg oryID)
{
// the category has changed, display a new sub-heading $laDownloadItem['description']
echo '<h4 class="eventLis tItem">Download Category: ' . substr_replace( $laDownloadItem['description'], strtoupper(subs tr($laDownloadI tem['description'], 0, 1)), 0, 1) . '</h4>' ;
// change the meta data
$lnCurrentCateg oryID = $lnNextCategory ID ;
}

break ;

case 1: // event
$lnNextCategory ID = $laDownloadItem['topicID'] ;
if($lnNextCateg oryID != $lnCurrentCateg oryID)
{
// the category has changed, display a new sub-heading
echo '<h4 class="eventLis tItem">Event: ' . substr_replace( $laDownloadItem['title'], strtoupper(subs tr($laDownloadI tem['title'], 0, 1)), 0, 1) . '</h4>' ;
// change the meta data
$lnCurrentCateg oryID = $lnNextCategory ID ;
}

break ;

case 2: // module
$lnNextCategory ID = $laDownloadItem['topicID'] ;
if($lnNextCateg oryID != $lnCurrentCateg oryID)
{
// the category has changed, display a new sub-heading
echo '<h4 class="eventLis tItem">Module: ' . substr_replace( $laDownloadItem['title'], strtoupper(subs tr($laDownloadI tem['title'], 0, 1)), 0, 1) . '</h4>' ;
// change the meta data
$lnCurrentCateg oryID = $lnNextCategory ID ;
}

break ;
}
// write the stuff that's the same for each type - the link - url, name and the onclick code using the ID to update the association between leader and download
// note the update is only for leaders - so another hasAccess check is required.
echo '<p class="linkList ">' ;
if ($loUser->hasAccess(1) )
{
echo '<a id="' . $laDownloadItem['ID'] . '" "class="inlineL ink" href="' . $laDownloadItem['url'] . '" title="' . $laDownloadItem['name'] . '" onclick="dataUp date(1, ' . $laDownloadItem['ID'] . ')">' . $laDownloadItem['name'] . '</a><br />';
}
else
{
echo '<a class="inlineLi nk" href="' . $laDownloadItem['url'] . '" title="' . $laDownloadItem['name'] . '">' . $laDownloadItem['name'] . '</a><br />';
}
echo '</p>';

}
echo '<br />' ; // just to give it some room to breathe
}
else
{
echo '<p>' ;
echo 'There are currently no downloads available. ' ;
echo 'New information is loaded regularly so please check back soon.' ;
echo '</p>' ;
echo '<p>' ;
echo 'In the mean time please feel free to browse around the rest of the site.' ;
echo '</p>' ;
}
}
else
{
echo '<p>' ;
echo 'Unfortunately this page was accessed incorrectly.' ;
echo '</p>' ;
echo '<p>' ;
echo 'There are 3 valid ways into this page - via Resources, Leaders Forum and Training. ';
echo 'It is essential that you are a registered with the site and that you are currently logged in.' ;
echo '</p>' ;
}
}
else
{
echo '<p>' ;
echo 'Access to this area of the site is for members only. If you are a member already please log in.' ;
echo '</p>' ;
echo '<p>' ;
echo 'If you would like to join the Christian Leadership Foundation please take a moment to complete the <a class="inlineLi nk" href="applicati on.php?step=1" title="Applicat ion Form">form</a>.' ;
echo '</p>' ;
}
include("includ e/pageFooter.php" );
?>

[/php]

On downloading a file the idea is that a record is either updated in or added to the database. The purpose of this is so that at a later date I can make recommendations within the members area based on the category of downloads they have been interested in previously. A simplistic recommendation system.

In order to do this the link to the download file has an onClick that calls a JavaSacript function

Expand|Select|Wrap|Line Numbers
  1. function dataUpdate(pnType, pnItem)
  2. {
  3.     GetXmlHttpObject() ;
  4.     gcUrl = "../lib/dataupdateajax.php?type=" + pnType + "&item=" + pnItem 
  5.     gcItemID = pnItem
  6.     goXMLHTTP.onreadystatechange = function()
  7.     { 
  8.         if (goXMLHTTP.readyState==4 || goXMLHTTP.readyState=="complete")
  9.         { 
  10.             alert("Enjoy");
  11.         }
  12.     }  ;
  13.     goXMLHTTP.open("GET",gcUrl,true) ;
  14.     goXMLHTTP.send(null) ;
  15. }    
  16.  
This is getting called - the alert tells me that. However, the URL listed is not being called. I have used this methodology for form validation previously without problem.

I know it's not getting into the PHP code because I have echo lines in there before any conditions are tested and they don't execute.

The basic problem is that I can't get the php file dataupdateajax. php to load.

Any suggestions on this or alternative ways to build a recommendation system will be greatly appreciated.

Cheers
nathj
Sep 4 '07 #1
21 2472
ak1dnar
1,584 Recognized Expert Top Contributor
There is a ";" on line number 12 on the javascript. what is it really.
Sep 4 '07 #2
nathj
938 Recognized Expert Contributor
There is a ";" on line number 12 on the javascript. what is it really.
Hi Ajaxrand,

That's what it is. Should this be removed do you think?

Cheers
nathj
Sep 4 '07 #3
ak1dnar
1,584 Recognized Expert Top Contributor
Hi Ajaxrand,

That's what it is. Should this be removed do you think?

Cheers
nathj
Really I didn't go through the your php coding yet. That semicolon doesn't makes any sense to me. If you are thinking there is a issue with the URL of the Server script too, try the absolute URL, instead the relative.
Sep 4 '07 #4
nathj
938 Recognized Expert Contributor
Really I didn't go through the your php coding yet. That semicolon doesn't makes any sense to me. If you are thinking there is a issue with the URL of the Server script too, try the absolute URL, instead the relative.
Hi Ajaxrand,

I have removed the semi colon and I have altered the url to be the full path. However, this has made no noticeable difference.

I have checked the case of the php file in the code and on the server to ensure they are the same and I've been caught out by that before.

As far as I can tell the onclick event is firing nicely and the javascript fnction is executing but the XMLHTTP object is not actually running the PHP code in dataupdateajax. php.

I have used code like this previously with no problems so I'm completely flumoxed by this issue.

I have also watched the code using Firebug and I can see now errors there.

Any further thoughts at all?

Cheers
nathj
Sep 4 '07 #5
jx2
228 New Member
pnType and pnItem are not url encoded that might cose problems (e.g. if there is a space in it)
regards
jx2
Sep 4 '07 #6
nathj
938 Recognized Expert Contributor
pnType and pnItem are not url encoded that might cose problems (e.g. if there is a space in it)
regards
jx2
Hi jx2,

I'll add some code to ensure there are no spaces but at present they equate to 1 and 3 respectively.

I should have said that in my original post - sorry.

Cheers
nathj
Sep 4 '07 #7
ak1dnar
1,584 Recognized Expert Top Contributor
Hi jx2,

I'll add some code to ensure there are no spaces but at present they equate to 1 and 3 respectively.

I should have said that in my original post - sorry.

Cheers
nathj
Please post back your javaScript coding that you have used to create XMLHttpRequest. and the calling JS function for the Php.
Sep 4 '07 #8
nathj
938 Recognized Expert Contributor
Please post back your javaScript coding that you have used to create XMLHttpRequest. and the calling JS function for the Php.
Ajaxrand,

Heres the html line that calls the javascript:
[html]<a id="3"class="in lineLink"href=" http://www.christianle adership.org.uk/m3u/eddiegibbs_sess ion1.m3u"title= "What Good Looks Like - Session 1(mp3)"onclick= "dataUpdate (1, 3)">What Good Looks Like - Session 1(mp3)</a>
[/html]

This is generated in PHP based on records in a database.

The function that is called in the above code is:
Expand|Select|Wrap|Line Numbers
  1. function dataUpdate(pnType, pnItem)
  2.  
  3. {
  4.  
  5.     GetXmlHttpObject() ;
  6.  
  7.     gcUrl = "http://www.christianleadership.org.uk/lib/dataupdateajax.php?type=" + pnType + "&item=" + pnItem 
  8.  
  9.     gcItemID = pnItem
  10.  
  11.     goXMLHTTP.onreadystatechange = function()
  12.  
  13.     { 
  14.  
  15.         if (goXMLHTTP.readyState==4 || goXMLHTTP.readyState=="complete")
  16.  
  17.         { 
  18.  
  19.             alert("Enjoy"); // needs to change to something else
  20.  
  21.         }
  22.  
  23.     } 
  24.  
  25.     goXMLHTTP.open("GET",gcUrl,true) ;
  26.  
  27.     goXMLHTTP.send(null) ;
  28.  
  29. }        
  30.  
This instantiates the goXMLHTTP object via the following JavaScript
(I use this function in other places and it works fine.)

Expand|Select|Wrap|Line Numbers
  1. function GetXmlHttpObject()
  2.  
  3.  
  4.     if (window.XMLHttpRequest)
  5.  
  6.     {
  7.  
  8.         goXMLHTTP=new XMLHttpRequest()
  9.  
  10.     }
  11.  
  12.     else if (window.ActiveXObject)
  13.  
  14.     {
  15.  
  16.         goXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP")
  17.  
  18.     }     
  19.  
  20.     if (goXMLHTTP==null)
  21.  
  22.     {
  23.  
  24.         alert ("Browser does not support HTTP Request")
  25.  
  26.          return    
  27.  
  28.     } 
  29.  
  30.  
I think thats all the code you asked for, if there's anything else let me know and I'll pos it happily - I really need some help with this as it's driving me mad.

Cheers
nathj

PS Are there specific code tags for javascript?
Sep 5 '07 #9
ak1dnar
1,584 Recognized Expert Top Contributor
Hi nathj,
Once you created a variable/object on a function, you have to return them to calling function. then Only you can use them.

check out this:

Expand|Select|Wrap|Line Numbers
  1. function GetXmlHttpObject()
  2. {
  3. if (window.XMLHttpRequest)
  4. {
  5. goXMLHTTP=new XMLHttpRequest()
  6. }
  7. else if (window.ActiveXObject)
  8. {
  9. goXMLHTTP=new ActiveXObject("Microsoft.XMLHTTP")
  10. }
  11. if (goXMLHTTP==null)
  12. {
  13. alert ("Browser does not support HTTP Request")
  14. return
  15. }
  16. return goXMLHTTP; // This Line is Missing in yours
  17.  
Sep 5 '07 #10

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

Similar topics

2
2056
by: christopher.secord | last post by:
I would like have a little "loading..." tab not unlike the one that gmail uses and I would like to display that tab while an ajax call is made. The javascript to display the tab works. The javascript to hide the tab works. But when I put the two together inside the function that calls the ajax service, they don't work. What seems to happen is that calls to change DOM object properties are queued up, and then all executed...
5
2302
by: adhag | last post by:
Hi I am faced with quite a challenge. I need to open a 70-100 meg file and be able to chunk it out using AJAX back to the client but that isn't my problem really. What I need to do is open the file and get pieces of it out without loading the entire thing into memory. The pieces themselves are random although of a fixed size. If I tried to read an entire file into a stirng and parse pieces out I use too much memory and if I use the...
10
2214
by: Simon | last post by:
Hi, Sorry for the cross posting, but I think it applies to both languages. As we all know, JavaScript is client side and php is server side, (the php code is 'allowed' to do stuff on the server that JavaScript cannot). The problem with php is that it timeout after a while, (and the user also has no clue as to what is going on for a long time). I need to run a script on the server that could take a very long time.
3
2138
by: niks | last post by:
I'm a javascript novice trying to write a client-side program that needs to examine the text of some included code. Suppose you have the following snippet in an html document: <script id="included" src="inc.js" type="text/javascript"></script> I want to be able to write a function along the following lines: function inspect() {
2
1775
by: obstinate | last post by:
hello all, i'm a newbie in ajax. now i work in team that developed an online map site which is similar like google map. we use <div> to contain the map. This div contains map image tiles and this map can be dragged like google map, when we drag this map, the map will request images with ajax. At this point, both in IE 6 an firefox has no problem to load the map image tiles. After that, we want to create icons that can be clicked,
7
1392
by: tader | last post by:
so i got this kind of ajax script function browserio_tipas() { var tipas; if (window.ActiveXObject) { tipas = new ActiveXObject("Microsoft.XMLHTTP"); } else if (window.XMLHttpRequest) { tipas = new XMLHttpRequest(); }
1
1672
by: =?Utf-8?B?QU5lZWxpbWE=?= | last post by:
I am using the asp.net 2.0 wizard with 5 steps and would like to do the following: Load a different .ascx (control) into each of the 5 steps I can load them fine but I notice that every time I navigate between steps, the entire UI 'flickers'. By debugging the problem, I noticed that the page_load event for each of the controls is called each time I click 'Next' or 'Previous' in the wizard
4
2687
by: Nicolas R | last post by:
Hi all, Im trying to figure out how to display a 'loading' message when scripts are being executed, ie when the page is still not ready for interaction. This is for a web app which relies on javascript to insert dom elements and do stuff, so the user must know when everything is ready to interact with. I tried using an interval which checks for a specific variable (page_loading) and when its set to false then the 'loading' message is...
7
2301
by: musther | last post by:
I have a set of calculators written in JS on a page: http://www.slymail.org/vinocalc.html The page is designed to be downloaded and used offline, or simply saved as a file for speed of access and reliability (not always on internet connections). Anyway, I would like to store a file on the server, which contains the current version number (such as 2.0). That's all the file will contain. Then when the page is loaded, it will access that...
29
24410
by: FreshRob | last post by:
I have been trying to fix this issue the whole of today and have gotten no where. I am developing a new website, and wanted it to display a webpage in lightbox and have an external page added to the main page, both worked successfully apart, but it wasn't until I combined them that I have had an issue. My aim was to have ajax add the external page into a div and then load lightbox from that div, though that is where the problem lies as I can...
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.