Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP/AJAX Question

PHP/AJAX Question
Thread Tools
Junior Member
Join Date: May 2000
Location: USA
Status: Offline
Reply With Quote
Feb 12, 2008, 01:18 AM
 
I'm new to PHP, and I have a quick question and hope somebody can help me out or point me in the right direction. I have a PHP page with a nav menu on the side. When you click one of the links, the corresponding section (written in PHP as well) gets loaded via AJAX into the main part of the page. I have a variable defined in a config.php file:

define('WEB_ROOT', $webRoot);

and each page that gets loaded does a quick check to make sure it's defined:

if(!defined('WEB_ROOT'))
exit;

The portions of the page that get loaded with the index.php file show up just fine, but if I put the above check into any of the pages that get loaded via AJAX into the main page nothing gets displayed. Ideas on how to remedy the situation?

Thanks in advance.
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness.
     
Mac Enthusiast
Join Date: Jan 2007
Status: Offline
Reply With Quote
Feb 14, 2008, 07:12 AM
 
I'll have a stab at this, though, I am no expert on this - pretty new myself.

When you initially load index.php, config.php is executed from the script. the WEB_ROOT variable is defined and used to when rendering index.php and then forgotten.

Later when you do your AJAX call, your page doesn't remember WEB_ROOT and therefore doesn't know what it is (hence the undefined). Maybe you can run config.php at the head of the pages that you are loading dynamically?

PHP may also have a function similiar to $_SERVER that points to the directory where the page exists. Though it seems like it would do the same thing that you're trying to do and may have the same problems. This is where I show my ignorance of PHP.
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 14, 2008, 09:03 AM
 
Yeah, each AJAX reload necessitates having to redefine your non-predefined variables... This includes your env variables and regular $whatever variables, but does not include your session variables or variables predefined by PHP.

For instance, if you were to do a:

<?php
session_start()
$_SESSION['myapp']['webroot'] = $webRoot;
?>

when you do an ajax reload:

<?php
session_start();
$webRoot = $_SESSION['myapp']['webroot'];
?>

would work. The $_SERVER variables that define paths that may be of use to you include:

$_SERVER['DOCUMENT_ROOT']
$_SERVER['SERVER_NAME']
$_SERVER['PHP_SELF']

These are available across ajax reloads too.
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Feb 14, 2008, 11:40 AM
 
The other replies here have basically nailed your problem. I would recommend either using the $_SERVER solution or having your pages all load config.php rather than using $_SESSION data as session should only contain state information, not constants like the document root. I'd also generally recommend against using ajax to load entire pages as you are describing. Doing a normal request for the page isn't typically going to be much heavier and it will maintain things the user expects like browser history and forward/backward navigation (although there are workarounds for this sort of thing). Mostly just my opinion on how technology should be used, though.
Travis Sanderson
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 14, 2008, 12:27 PM
 
I agree with redJag, good points there...

While there are reasons to load pages via AJAX calls (I do this to avoid my Flash media player from being interrupted, for instance), you are increasing the complexity of your site by doing this. For instance, Google's spider does not support Javascript. Therefore, you will need to make sure these pages are accessible by those without Javascript support enabled in their browsers. One technique I've come up for doing this is a Javascript function that runs onload that converts the standard hrefs within a div tag to your JS AJAX function. The beauty of this is that Javascript-less browsers will simply ignore this onload/JS call, so you won't have to create two separate versions of your pages.

I also agree that session vars are usually not the best choice for constants - especially those which expose sensitive information about the server hosting the site. You probably don't want to store the document root in session/cookie data. That was not a smart suggestion on my part.
     
Dedicated MacNNer
Join Date: Jul 2002
Status: Offline
Reply With Quote
Feb 15, 2008, 10:18 PM
 
Doesn't the exit; statement cause PHP to stop serving data beyond that point, or just code between the <?php ?> declarations?
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 15, 2008, 11:38 PM
 
The former, I think - script ceases execution.
     
Dedicated MacNNer
Join Date: Jul 2002
Status: Offline
Reply With Quote
Feb 16, 2008, 04:03 PM
 
Okay, so, if he's doing ...

if(!defined('WEB_ROOT'))
exit;

... at the start of every webpage, then it's no wonder the pages are coming up blank.
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Feb 17, 2008, 04:47 PM
 
He was expecting WEB_ROOT to be defined, when it wasn't, so yes that is why the pages are coming up blank. I think he knew that, but was curious why it wasn't defined when he had defined it on the other script.
Travis Sanderson
     
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Feb 17, 2008, 04:55 PM
 
redJag: and this has been answered, no?
     
Senior User
Join Date: Dec 2002
Status: Offline
Reply With Quote
Feb 18, 2008, 10:10 PM
 
right! I don't know why this thread is still going.. hehe
Travis Sanderson
     
Junior Member
Join Date: May 2000
Location: USA
Status: Offline
Reply With Quote
Mar 8, 2008, 07:28 PM
 
Thanks for all the responses.

On a somewhat related note, I found some frameworks that do exactly what I was looking for - a way to encapsulate certain parts of my site and share information between them. One is the Zend Framework. The other is Code Igniter.
When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:38 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2