如何加载一个PHP包括到页面中使用Ajax虽然页面加载加载、页面、PHP、Ajax

2023-09-10 17:50:36 作者:千万种野心

我有一个PHP包括它需要一段时间来加载,因为PHP有​​获取大量的数据。我不想放慢整个网页加载等待这包括让我怎么可以加载这个包括与阿贾克斯?我不想让AJAX通过一个按钮来触发点击我只是希望它装载了包括在页面加载,这样,如果你看一下下面一些更多的HTML内容的我的例子显示虽然inlude。 PHP仍在加载研究。

 < HTML>
< HEAD>
< /头>
<身体GT;
    一些HTML内容
    <脚本>
        阿贾克斯负荷include.php;
     < / SCRIPT>
    一些更多的HTML内容
< /身体GT;
< / HTML>
 

解决方案

如果你正在使用jQuery,您可以使用自己的AJAX的一个调用从include.php加载HTML。例如,你可以使用负载()功能。

jsp使用ajax加载子页面,如何提交子页面的表单

例如:

 < D​​IV ID =内容>< / DIV>

<脚本>
$(文件)。就绪(函数(){
    $(#内容)加载(/ yourpath / include.php)。
});
< / SCRIPT>
 

I have a php include which takes awhile to load because PHP has to fetch a lot of data. I don't want to slow the whole webpage loading waiting for this include so how can I load this one include with ajax? I don't want the ajax to be triggered by a button click I just want it to load up the include as the page is loading so that if you look at my example below 'Some more html content' would be displayed whilst the inlude.php is still loading in.

<html>
<head>
</head>
<body>
    Some html content
    <script>
        Ajax load 'include.php';
     </script>
    Some more html content
</body>
</html>

解决方案

If you're using jQuery, you could you use one of their ajax calls to load your html from include.php. For example you could use the load() function.

For example:

<div id="content"></div>

<script>
$(document).ready(function() {
    $("#content").load("/yourpath/include.php");
});
</script>