简体   繁体   中英

Not able to load php page on ajax call

I am trying to load a php page when I click the navigation

    <ul id="navigation">
    <li><a href="#page1">Page 1</a></li>
    <li><a href="#page2">Page 2</a></li>
    <li><a href="#page3">Page 3</a></li>
    <li><a href="#page4">Page 4</a></li>
    <li><img id="loading" src="img/ajax_load.gif" alt="loading" /></li>
    </ul>

Here is the javascript that I used.

var default_content="";

$(document).ready(function(){

    checkURL();
    $('ul li a').click(function (e){

            checkURL(this.hash);

    });

    //filling in the default content
    default_content = $('#pageContent').html();


    setInterval("checkURL()",250);

});

var lasturl="";

function checkURL(hash)
{
    if(!hash) hash=window.location.hash;

    if(hash != lasturl)
    {
        lasturl=hash;

        // FIX - if we've used the history buttons to return to the homepage,
        // fill the pageContent with the default_content

        if(hash=="")
        $('#pageContent').html(default_content);

        else
        loadPage(hash);
    }
}


function loadPage(url)
{
    url=url.replace('#page','');

    $('#loading').css('visibility','visible');

    $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        dataType: "php",
        success: function(msg){

            if(parseInt(msg)!=0)
            {
                $('#pageContent').html(msg);
                $('#loading').css('visibility','hidden');
            }
        }

    });

}

This is what I have in load_page.php

<?php

if(!$_POST['page']) die("0");

$page = (int)$_POST['page'];

if(file_exists('demo_'.$page.'.php'))
echo file_get_contents('demo_'.$page.'.php');

else echo 'There is no such page!';
?>

在此输入图像描述

So basically it is not reading the php code as php.

Irrespective of the question, all I am trying to do is to run the following sql based on the value of clicked nav.

$result = mysql_query("
SELECT q.*, IF(v.id,1,0) AS voted
FROM quotes AS q
LEFT JOIN quotes_votes AS v 
ON  q.id = v.qid
    AND v.ip =".$ip."
    AND v.date_submit = '".$today."' where q.bgc= "nav_value" order by rating DESC
");

file_get_contents CAN't handle PHP. You only read the file content. If you want to use the PHP, include it with include , require_once or other including functions:

<?php

if(!$_POST['page']) die("0");

$page = (int)$_POST['page'];

if(file_exists('demo_'.$page.'.php'))
require_once('demo_'.$page.'.php');

else echo 'There is no such page!';
?>

Try

 $.ajax({
            type: "POST",
            url: "load_page.php",
            data: {'page':url}
            dataType: "text",

            success: function(msg){

                if(parseInt(msg)!=0)
                {
                    $('#pageContent').html(msg);
                    $('#loading').css('visibility','hidden');
                }
            }

        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM