无法在ajax调用上加载php页面

时间:2014-04-28 05:39:43

标签: javascript php jquery ajax

我在点击导航时尝试加载php页面

    <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>

这是我使用的javascript。

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');
            }
        }

    });

}

这就是我在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!';
?>

enter image description here

所以基本上它不是像PHP那样读取PHP代码。

无论问题如何,我所要做的就是根据点击导航的值运行以下sql。

$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
");

2 个答案:

答案 0 :(得分:2)

file_get_contents CAN无法处理PHP。您只阅读文件内容。如果您想使用PHP,请将其包含在 include require_once 或其他功能中:

<?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!';
?>

答案 1 :(得分:0)

尝试

 $.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');
                }
            }

        });