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

Asked

Viewed 162 times

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 an example above, my question is: in Ajax, how do I access the home page.php and it will only exist when it is passed in the URL

I’m doing like this:

script js.

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

home php.

echo $_POST['nome'];

However in Alert it is returning me the whole HTML :( .. pf, help me. I wanted to at least write in the body the value passed in data in ajax.

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

  • Have you tried pointing directly to the page url:'paginas/home.php', in ajax? When do you have include_once 'paginas/'.$_GET['pagina']; That’s what you want in the url ajax.

1 answer

0

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;   
    }
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.