Javascript AJAX从PHP后端加载的页面
我想做的是从不同的页面加载信息,而不必刷新整个主页 您能告诉我如何调整此代码以加载不同名称的文件,如about.html和project.html吗 注意:此代码仅用于加载“page..html”文件 以下是php文件:Javascript AJAX从PHP后端加载的页面,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我想做的是从不同的页面加载信息,而不必刷新整个主页 您能告诉我如何调整此代码以加载不同名称的文件,如about.html和project.html吗 注意:此代码仅用于加载“page..html”文件 以下是php文件: if(!$_POST['page']) die("0"); $page = (int)$_POST['page']; if(file_exists('pages/page_'.$page.'.html')) echo file_get_contents('pages/pa
if(!$_POST['page']) die("0");
$page = (int)$_POST['page'];
if(file_exists('pages/page_'.$page.'.html'))
echo file_get_contents('pages/page_'.$page.'.html');
else echo 'There is no such page!';
更换这条线
if(file_exists('pages/page_'.$page.'.html'))
用这个
if(file_exists('pages/'.$page.'.html'))
您可以发出多个ajax请求这是jquery加载方法:
$( "#result" ).load( "ajax/test.html", function() {
alert( "Load was performed." );
});
这样做2倍,你的财富!
请记住,要使其发挥作用,您将需要jquery库
我在我的网站上做了类似的事情,下面是我的代码:
window.setInterval("check()",60000);
//request information notice is inside a function called check() (it's not required to put inside function I only do this if I will be making the same request multiple time throughout the program)
function check() {
var request = $.ajax({
url: "file.php",
type: "POST",
dataType: "html"
});
request.done(function(msg) {
//when request is done:
$(".wheretoputnewdata").html(msg);
});
request.fail(function(jqXHR, textStatus) {
//if request failed do this:
alert( "Request failed: " + textStatus );
});
}
只需在页面名称前加上page_u,并确保它们位于页面文件夹中,我怀疑这与您的页面名称重复。不过,我认为,现在已经修复,它将重新打开。现在我将暂缓结束这个问题,但请不要在将来创建重复的问题-修复一个问题比再次提问更好。
window.setInterval("check()",60000);
//request information notice is inside a function called check() (it's not required to put inside function I only do this if I will be making the same request multiple time throughout the program)
function check() {
var request = $.ajax({
url: "file.php",
type: "POST",
dataType: "html"
});
request.done(function(msg) {
//when request is done:
$(".wheretoputnewdata").html(msg);
});
request.fail(function(jqXHR, textStatus) {
//if request failed do this:
alert( "Request failed: " + textStatus );
});
}