Basic navigation with PHP not working
Вопрос
I have the following PHP page:
index.php
if( isset($_POST['location']) ) {
$location = $_POST['location'];
} else {
$location = 'home';
}
require 'header.php';
switch ($location) {
case "home": require 'home.php'; break;
case "about": require 'about.php'; break;
case "products": require 'products.php'; break;
case "budget": require 'budget.php'; break;
case "contact": require 'contact.php'; break;
case "help": require 'help.php'; break;
default: require 'home.php';
}
require 'footer.php';
?>
Inside the HEADER I have:
header.php
<body class="<?php echo $location; ?>">
<div id="navmenu">
<ul id="menu">
<li><a id="page_item_1" href="?location=home">Home</a></li>
<li><a id="page_item_2" href="?location=about">About</a></li>
<li><a id="page_item_3" href="?location=products">Products</a></li>
<li><a id="page_item_4" href="?location=budget">Budget</a></li>
<li><a id="page_item_5" href="?location=contacto">Contact</a></li>
<li><a id="page_item_6" href="?location=help">Help</a></li>
</ul>
</div>
But nothing works... The location POST changes but the body's class doesn't change. And either the body's content require (doesn't load home/about/etc.php).
What am I doing wrong? Thanks.
Решение
You're passing stuff via GET
, not POST
.
if( isset($_GET['location']) ) {
$location = $_GET['location'];
} else {
$location = 'home';
}
For future reference, you can print the GET
and POST
variables to see which contains what:
print_r($_GET);
And while on the subject, $_REQUEST
contains them both:
print_r($_REQUEST);
To send POST data, you have to send them with a form - irrelevant here. What you see in URLs query strings is GET
data, not POST
.
Другие советы
Change your post to get.. i.e.
if( isset($_GET['location']) ) {
$location = $_GET['location'];
} else {
$location = 'home';
}
You use GET not POST + why dont use $location + "php"? :
if( isset($_GET['location']) ) {
$location = $_GET['location'];
} else {
$location = 'home';
}
require 'header.php';
require $location . '.php';
require 'footer.php';
Не связан с StackOverflow