Ajax URL changes if the pages come by variables by URL?

0

Good people, I have an index.php that calls my other pages as requested

Ex:

index.php:

<a href="home.php">Home</a>

if( isset($_GET['pagina']) ){
     include_once 'paginas/'.$_GET['pagina'];
}

It was just one example above, my question is: in Ajax, how do I reach the home.php page and it will only exist when it is passed in the URL

I'm doing this:

script.js

 $.ajax({
         type:'post',
         url:'index.php?pagina=home',
         data:{nome:'Nome'},
         success: function (data){
              alert(data);
         }
    });

home.php

echo $_POST['nome'];

  

NOTE: If I could not be clear, please ask in the comments information that helped you respond to me

    
asked by anonymous 31.01.2016 / 08:10

1 answer

0
  

SOURCE AND DEMO link

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><styletype="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;   
    }
}
?>
    
03.02.2016 / 20:25