Jump to content

'How' can I show info in one div from a menu called in another (with example)


mlsherrod

Recommended Posts

HI! I'm new to PHP (like a lot of us) but here's my question and example:

 

Question, I want my index.php page to have a menu from one div, that changes content in another div.

 

To start, my basic index.php page calls up & renders my drop down menu from another file, via :<?php include('menu.php');?>

 

Here's a basic menu in one div:

 

<div id="container1">
    <ul id="menu" class="topmenu">
      	<li class="topfirst"><a href="home.html">Home</a></li>
<li class="topmenu"><span>About us</span>
<ul>
	<li><a href="?page=about.html">About</a></li>
	<li><a href="?page=history.html">History of AHTS</a></li>
	<li><a href="?page=faq.html">FAQ's</a></li>
	<li><a href="?page=brochure.pdf">Brochure (PDF)</a></li>
</ul></li>
</div>

 

 

On my same index.php page below my menu, I have another div to call up the menu selection:

 

<div id="container2">
<?php
     if (isset($_GET['page']))
        { include($_GET['page']);}
    else{ echo "select a page";}?>
</div>

 

So my basic question is, I'm getting my pages to propagate in container2 div below from menu clicks, but when I first go to my site (index.php), I see my menu, but now home page. I can't get my home.html file to show automatically like one can do with iFrames (like below).  Any suggestions?

<iframe name="iframe1" src="i_index.html"></iframe>

 

Many thanks!

 

Link to comment
Share on other sites

You need to set a default value, which contains the name of your home page. Then, if the user haven't selected a specific page, you show the default.

Something like this, in other words:

<?php

// Set default page.
$page = "home";

// If the user requested a different page, validate and set it.
if (isset ($_GET['page'])) {
    // Using Regular Expressions to only allow letters a-z (lowercase), to prevent attackers from doing anything bad.
    $page = preg_replace ("/[^a-z]/", '', $_GET['page']);
}

// If the page exists in the include folder, and is readable, read its contents.
$path = "include/{$page}.html";
if (is_file ($path) && is_readable ($path)) {
    $output = file_get_contents ($path);
} else {
    // Send a 404 HTTP error.
    header ("HTTP/1.0 404 Not Found");
    $output = "404 File Not Found";
}

// Do whatever else you want below.
?>
<doctype html>
<html>
<head>

<title>Title of page</title>

</head>
<body>

<menu></menu>
<div id="content"><?php echo $output; ?></div>

</body>
</html>

 

Note that I've limited what the user can write in the URL to only letters, and limited the inclusion to happen in one folder only and with a set extension. This is to prevent attackers from reading files that they shouldn't, and thus exploit your code to expose stuff like passwords and other things you don't want them to have.

Link to comment
Share on other sites

I do not recommend using a switch-case statement, simply because you'll have to edit it every time you wanted to change the name or number of pages to be included.

It would also add a lot of unnecessary code, where you simply map a GET parameter to a filename. So if you're really set on doing it this way, then using an array to hold the mappings would be far preferable:

$pages = ('home' => 'main', 'about' => 'about_us', 'products' => 'products_list');
if (isset ($pages[$page])) {
    $page = $pages[$page];
} else {
    // Force a 404 error from the inclusion test. Assumes "file not found.html" is not a valid file.
    $page = "file not found";
}

However, this code would come in addition to the code I wrote above. As the checks are still needed to ensure that the files actually does exists on the server, and that they can indeed be read.

Link to comment
Share on other sites

So If i go with ChristianF's suggestion I have a new problem, suggestions?

My home page shows up, though now I have a different issue, in that my links are not propagating...

I made  very simple site to play with it:

http://hikealabama.org/testsite

 

menu.html code:

<doctype html>
<html>
<head>
</head>
<body>
<div id="menu">
    <ul id="css3menu1" class="topmenu">
      	<li class="topfirst"><a href="?page=/">Home</a></li>
<li class="topmenu"><a href="?page=about.html"><span>About us</span></a>
<ul>
	<li><a href="?page=about.html">About</a></li>
	<li><a href="?page=abouthistory.html">History of AHTS</a></li>
	<li><a href="?page=aboutfaq.html">FAQ's</a></li>
	<li><a href="?page=aboutahtsinthenews.html">AHTS in the News</a></li>
</ul></li>
</div>
</body>
</html>

 

home.html code:

<doctype html>
<html>
<head>

<title>hello</title>

</head><body>
just saying hello
</body>
</html>

 

Index.php code:

<?php
    
    // Set default page.
    $page = "home";
    
    // If the user requested a different page, validate and set it.
    if (isset ($_GET['page'])) {
        // Using Regular Expressions to only allow letters a-z (lowercase), to prevent attackers from doing anything bad.
        $page = preg_replace ("/[^a-z]/", '', $_GET['page']);
    }
    
    // If the page exists in the include folder, and is readable, read its contents.
    $path = "{$page}.html";
    if (is_file ($path) && is_readable ($path)) {
        $output = file_get_contents ($path);
    } else {
        // Send a 404 HTTP error.
        header ("HTTP/1.0 404 Not Found");
        $output = "404 File Not Found";
    }
    
    // Do whatever else you want below.
    ?>

<body>

<menu>
<?php include('menu.html');?>
</menu>
<div id="content"><?php echo $output; ?></div>

</body>

Link to comment
Share on other sites

Check the resulting HTML code, and I think you'll see a problem. You've clearly not quite understood how this process works.

 

Also, the problem you have is related to the filenames you've attempted to use. As such I recommend that you re-read my post, and go through the code line by line. If you want to use some code, it is quite imperative that you understand every detail of what it does.

Link to comment
Share on other sites

Check the resulting HTML code, and I think you'll see a problem. You've clearly not quite understood how this process works.

I took out the include folder, I know that. So I'll look at that direction too.

 

Also, the problem you have is related to the filenames you've attempted to use. As such I recommend that you re-read my post, and go through the code line by line. If you want to use some code, it is quite imperative that you understand every detail of what it does.

My understanding was I did not need html headers or the like, and simply name all files *.php

Of course I am trying to stay within your limits of only .html files being accepted, so I modified everything for that.  I thought I was wrong to do such.  Imma gonna study this more tonight

Link to comment
Share on other sites

I think you'll find out a lot more about how the code works, if you step through the code with some data. If you don't have a debugger, you can also accomplish this with using echo or var_dump () after each line. That'll visualize the process for you, and help your understanding a lot.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.