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?
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.