Page 1 of 1
buttons, headers, navigation
Posted: Mon Jan 17, 2011 1:48 pm
by DrStan
Hi all,
I'm pretty new to php.
I have an
index.php page that has a login (submit-type button). Successful login leads to code, including
Code: Select all
include("header.php")
include("show_intro.php")
The header.php has a series of submit-type buttons, which I want to use for navigation.
"show_intro.php" shows (below the header navigation buttons of header.php) some content.
Clicking a (header) navigation submit button should lead to the same header.php, but with a different show_xxx.php (e.g., for my photos, "show_florida,php, show_georgia.php, etc).
On the initial page, index.php, I can handle the submit-button click. What I don't know is how to handle checking for one of the navigation submit-button clicks, and in particular, having such a click result in the paired includes: header.php and show_xxx.php.
Should the code to handle the clicks be in header.php -- wouldn't this make header.php self-referencing, circular or some other problem??
Needless to say, in addition to explanation, specific code would be most welcome.
Many thanks,
-- Stan
Re: buttons, headers, navigation
Posted: Mon Jan 17, 2011 2:17 pm
by greyhoundcode
That sounds to me like you are using a form as the basis of your navigation - if so you should inspect the
$_POST or
$_GET arrays as appropriate. For instance, if your navigation includes something like
Code: Select all
<form method="post">
<select name="destination">
<option value="kamchatka">Kamchatka</option>
<!-- etc -->
Then you will want to look at the value of
$_POST['
destination'] and redirect or include further files as required.
* If following your test against a $
_POST or
$_GET element you go on to include another file then be sure to check it against a whitelist.
Re: buttons, headers, navigation
Posted: Mon Jan 17, 2011 2:59 pm
by DrStan
Thanks, Greyhoundcode, for your quick response.
1. I'm not sure what I'd need to whitelist. There's only a set of submit-type buttons on the header.php, which I use for navigation, no text entry.
2. Again, INSIDE the header.php, what code should I be using to (a) test for which button has been clicked, and (b) when the clicked button is found, how to code that I want to display
Code: Select all
include("header.php")
include("show_xxx.php")
(for the selected content, e.g., "show_georgia.php").
For (a), I suppose I just do a nested series of
, etc. But can I then, in the curly brackets for each isset, just have the two insert statements (above)?? Where would, for example, the closing </html> go?? After each pair of includes??
Very confused...
Stan
Re: buttons, headers, navigation
Posted: Tue Jan 18, 2011 12:04 pm
by greyhoundcode
Well, there are different ways to skin a cat. Let's say the navigation form in your header sends a post variable like
$_POST['nunavut'] to your designated action address (lets call that
index.php).
Using
if statements you might do something like this.
Code: Select all
$incFile = null;
// Possible destinations
if (isset($_POST['labrador']))
$incFile = 'labrador';
elseif (isset($_POST['newfoundland']))
$incFile = 'newfoundland';
elseif (isset($_POST['nunavut']))
$incFile = 'nunavut';
elseif (isset($_POST['yukon']))
$incFile = 'yukon';
// No match?
if (is_null($incFile))
{
include 'header.php';
include 'nomatch.php';
}
// Positive match?
else
{
include 'header.php';
include "show_$incFile.php";
}
An alternative approach could be something like this:
Code: Select all
// Possible destinations
$destinationList = array(
'labrador' => null,
'newfoundland' => null,
'nunavut' => null,
'yukon' => null,
);
// Check if $_POST contains any of the above
$destination = array_keys(array_intersect_key($destinationList, $_POST));
// No match?
if (count($destination) == 0)
{
include 'header.php';
include 'nomatch.php';
}
// Positive match?
else
{
// Requested include filename
$includeFile = "show_$destination[0].php";
// Now include the header and the requested destination file
include 'header.php';
include $includeFile;
}
So basically your navigation form always sends to index.php which in turn handles what to show your visitor.
Re: buttons, headers, navigation
Posted: Tue Jan 18, 2011 12:47 pm
by greyhoundcode
DrStan wrote:Where would, for example, the closing </html> go?? After each pair of includes??
Lets say a typical page contains a
header (including your navigation form), a
main body (which might be one of your files like show_nunavut.php or whatever) and a
footer (copyright notice, etc).
Code: Select all
<?php
/*
* index.php
*/
// Your navigation form posts data to index.php.
// Logic is handled here (see above post)
// and we then include the required files
include 'header.php';
include 'show_yukon.php';
include 'footer.php';
Code: Select all
<!DOCTYPE HTML>
<html>
<!-- header.php -->
<head>
<title>My website name</title>
</head>
<body>
<form action="index.php" method="post">
<!-- If you are using submit buttons stick them here -->
</form>
Code: Select all
<!-- show_yukon.php -->
<h3>The Yukon is rich in gold.</h3>
<p> <em>You can make your fortune panning for gold in the Yukon.</em>
Millions of people have already exceeded, etc.</p>
Code: Select all
<!-- footer.php -->
<p>Copyright © 2011 Me</p>
</body>
</html>
Re: buttons, headers, navigation
Posted: Tue Jan 18, 2011 1:10 pm
by DrStan
Thanks to all for your responses. I'm using the isset approach in my Header.php file, and figured out where to put what. It's looking very good! I had to change the <form> statement to have the Header.php's <form> statement's action refer to Header.php, not the standard PHP_SELF (which was index.php), but once I figured this out, then with the if isset statements, all fell into place. Thanks again!