Change pages using PHP

AusQB

New Member
First of all, I'm sorry for cluttering up the forums with all these similar threads, but I keep running into new snags.


What I have here is a main <div> with nav buttons above. When I click the buttons (<td> with onclick function), I want to include() the respective php file into that div.

I've tried all sorts of scripting, but I can't find a solution. I figured the only way to change the contents of the div is to have a function (called from the nav button) that passes a keyword into a switch that will determine the code to write, which I would then insert into the div using the innerHTML attribute.

The problem is that the php statements are parsed before the script is run, so the file data is written out onto the page source. However, it's not even writing that to the div.


I'm sure that might be quite confusing, so please ask for clarification where necessary.
 
Last edited:

adx

New Member
Code:
<?php
if (isset($_REQUEST['tab'])) {
$tab = $_REQUEST['tab'];} 
else {$tab = 1;}
?>
<!DOCTYPE...

Code:
<?php
  switch ($tab) {
  case 1:
 ?>
 
   <div id="navbar">   
    <b>Tab 1</b>
    <a href="index.php?tab=2">Tab 2</a>
    <a href="index.php?tab=3">Tab 3</a>
   </div>
   <?php include("page1.php"); ?>
   
 <?php
  break;
  case 2:
 ?>   
 
   <div id="navbar">
    <a href="index.php?tab=1">Tab 1</a>
    <b>Tab 2</b>
    <a href="index.php?tab=3">Tab 3</a>
   </div>
   <?php include("page2.php"); ?>
   
 <?php
  break;
  case 3:     
 ?>
 
   <div id="navbar">
    <a href="index.php?tab=1">Tab 1</a>
    <a href="index.php?tab=2">Tab 2</a>
    <b>Tab 3</b>
   </div>
   <?php include("page3.php"); ?>
 
 <?php
  break;}
 ?>

I think that should do the trick. You can optionally cut out the actual navigation as it's only there so you can highlight the current tab.
 

AusQB

New Member
Thanks, I'll try that.

Just for clarification, is the second batch of code meant to encapsulate the nav bar and the main div area?

Also, why is the first batch of code before the document declaration?


EDIT: That's exactly what I was looking for, thanks. One thing though, how can I change what is initially displayed?

I actually don't need it for changing pages now, but rather for navigating to different levels of a single page. So once I click on one of the links in the "navbar", I want the content of the div to be replaced.
 
Last edited:

adx

New Member
I actually don't need it for changing pages now, but rather for navigating to different levels of a single page. So once I click on one of the links in the "navbar", I want the content of the div to be replaced.

You can include whatever you want, or simply write the code right in there. It won't change the page it'll just do a refresh and change the contents accordingly. If the code is on the same page, the page will be the same.
i.e. you can have three divs that are essentially the same and it'll swap between them just as you wanted.

Also it doesn't really matter what you put inside the code but anything outside will remain the same.

Code:
<div id="navbar">   
<a href="index.php?tab=1">Tab 2</a>
<a href="index.php?tab=2">Tab 2</a>
<a href="index.php?tab=3">Tab 3</a>
</div>

<?php
  switch ($tab) {
  case 1:
 ?>
   
  <div>something</div>
   
 <?php
  break;
 ...
 
Top