javascript - 无法通过 ajax 调用加载 php 页面

标签 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.ini。

无论问题如何,我想做的就是根据单击的导航值运行以下 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
");

最佳答案

file_get_contents 无法处理 PHP。您只读取文件内容。如果您想使用 PHP,请将其包含在 includerequire_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!';
?>

关于javascript - 无法通过 ajax 调用加载 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333267/

相关文章:

javascript - 大量的setTimeout()——怎么喝?

javascript - 在 Laravel 中是否有更简单的方法将数据从页面传递到模式?

php - InvalidArgumentException : The current node list is empty. PHP-Spider (DOMCrawler Symfony)

javascript - 使用 jQuery/JavaScript 将元素向右浮动

javascript - 使用javascript强制图像缓存

javascript - 使用 Angular 动画(ng-show)添加滑入和滑出动画

javascript - 将库转换为 JavaScript 闭包

javascript - backbone js 对我的联系人列表进行排序

javascript - 调整@bazmegakapa 的省略号脚本以供内联使用

javascript - 如何将类添加到 Medium Editor 中的当前事件段落?