SOURCE AND DEMO > http://phppot.com/demo/load-dynamic-content-using-jquery-ajax/
SQL
CREATE TABLE IF NOT EXISTS `paginas` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`url` varchar(255) NOT NULL,
`titulo` varchar(255) NOT NULL,
`corpo` text NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `pages` (`id`, `url`, `titulo`, `corpo`) VALUES
(1, 'index.php', 'Home', 'Bem vindo a home!!'),
(2, 'index.php?page=about', 'Sobre', 'Criamos ideias.'),
(3, 'index.php?page=Contact', 'Contato', 'Seu email: <input type="text" />');
INDEX.PHP
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$pages = $db_handle->runQuery("SELECT * FROM paginas");
?>
<html>
<head>
<title>Carregar HTML com jQuery AJAX</title>
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
<style type="text/css" media="screen">
body{width:610;}
#menu{background: #D8F9D3;height: 40px;border-top: #F0F0F0 2px solid;}
#menu input[type="button"]{margin-left: 2px;padding: 0px 15px;height: 40px;border: 0px;background: #F0F0F0;}
#output{min-height:300px;border:#F0F0F0 1px solid;padding:15px;}
</style>
<script type="text/javascript">
function getPage(id) {
$('#output').html('<img src="LoaderIcon.gif" />');
jQuery.ajax({
url: "get_page.php",
data:'id='+id,
type: "POST",
success:function(data){$('#output').html(data);}
});
}
getPage(1);
</script>
</head>
<body>
<?php
if(!empty($pages)) {
?>
<div id="menu">
<?php
foreach($pages as $page) { ?><input type="button" value="<?php echo $page["title"]; ?>" onClick="getPage(<?php echo $page["id"]; ?>);" /><?php }?>
</div>
<?php } ?>
<div id="output"></div>
</body>
</html>
GET_PAGE.PHP
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$pages = $db_handle->runQuery("SELECT * FROM paginas WHERE id = ".$_REQUEST['id']);
if(!empty($pages)) {
?>
<h3><?php echo $pages[0]['titulo'];?></h3>
<div><?php echo $pages[0]['corpo'];?></p>
<?php } ?>
DBCONTROLLER.PHP
<?php
class DBController {
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "meu_banco";
function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}
function connectDB() {
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}
function selectDB($conn) {
mysql_select_db($this->database,$conn);
}
function runQuery($query) {
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}
function numRows($query) {
$result = mysql_query($query);
$rowcount = mysql_num_rows($result);
return $rowcount;
}
}
?>
Have you tried pointing directly to the page
url:'paginas/home.php',
in ajax? When do you haveinclude_once 'paginas/'.$_GET['pagina'];
That’s what you want in theurl
ajax.– Sergio