当我点击导航时,我试图加载一个 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!';
?>
所以基本上它不会将 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,请将其包含在 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!';
?>
关于javascript - 无法通过 ajax 调用加载 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333267/