tags:

views:

309

answers:

5

This is a snippet from my index.php page, where I'm displaying a menu with three HTML links.

Selecting a menu item defines the page parameter, which the PHP templating logic then loads, populating into <div id="main">.

<div class="span-24" id="menu">
  <table>
    <tr>
      <td><a href="index.php?page=races">Races</a></td>
      <td><a href="index.php?page=runners">Runners</a></td>
      <td><a href="index.php?page=searchRunners">Search</a></td>
    </tr>
  </table>
<br/>
</div>

<!-- main content -->
<div class="span-24" id="main">
<?php 
  $MYROOT=$_SERVER['DOCUMENT_ROOT'];
  if(isset($_GET['page']))
  {
    $page=$_GET['page'];
    @ require_once($MYROOT."/$page.php");
  }
  else
  {
    @ require_once($MYROOT."/races.php");
  }
?>
</div>

This solution works fine, but I'm worried that as I add more pages and have more page parameters to track, this approach will become difficult to maintain with the various PHP require() / require_once() method calls.

I also have jQuery available and I was wondering what could be the benefits or drawbacks of just using a AJAX based query to render the HTML for <div id="main">.

I think using AJAX might be a better solution, since I'll have more flexibility in the page that I can call and which parameters I can pass within the AJAX call.

I know PHP and the AJAX can be used together, but if you were starting this type of project, how would you do it?

+8  A: 

You don't want to do this. AJAX is nice and all but this would be misusing it. It would not only needlessly make the website inaccessible to people with Javascript disabled (which I'm sure you're aware of), it would also kill the navigational standard of the web on your website. How could someone send a link to a friend to check out the Races page, then? How can I go back and forth between pages? There's workarounds to this, but it is not worth it. You are better suited to just re-factor your PHP code so it is dynamic while safe in including what you want.

Paolo Bergantino
A: 

if you use ajax you are still going to be talking to php that will need to set up for the task requested, so nothing really changes. I guess I am wondering why you are passing a parameter back to a single php script instead of perhaps invoking different php files like this:

<td><a href="races.php">Races</a></td>
<td><a href="runners.php">Runners</a></td>
<td><a href="searchrunners.php">Search</a></td>
Scott Evernden
+5  A: 

To expand on what Paolo said, Google and other search engines can't "crawl" any content loaded via javascript. That means only the static information on the page would get indexed, and your page ranking would be all but non-existent.

tj111
+1 forgot that very important point.
Paolo Bergantino
A: 

Make them separate pages. Use a Templating system. A very simple one to use is http://andy.greyledge.net/voot/index.php

You start out with a HTML template page with Replacers in it Like "%HEADER%" or %FOOTER% or %BODY%.

you import this page into a php page, set these variables and then render the page.

If you want to add more pages you add it to the Menu in the HTML template and it shows for every page.

And everything everyone else said is correct.

mmundiff
A: 

Another alternative would be to use an array of the pages you want to have:

<?php 
    $pages = array('page', 'races');
    $MYROOT=$_SERVER['DOCUMENT_ROOT'];
    if(isset($pages[$_GET['page']])){
        @require_once($_GET['page'].'.php');
    }
?>

But if you are going to do it this way, you might want to think about moving on to an MVC framework as it seems that that is what you are attempting to recreate

SeanJA