使用jQuery的'load()'加载带有内部PHP include函数的外部PHP文件 [英] Loading an external PHP file with an internal PHP include function by using jQuery's 'load()'

查看:67
本文介绍了使用jQuery的'load()'加载带有内部PHP include函数的外部PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个带有新闻"部分和较旧新闻"部分的网站.该新闻分别存储在服务器通过PHP加载的外部 PHP 文件中(服务器计数PHP文件的数量,并在新闻"部分显示最后一个文件(当前新闻),在旧新闻"部分显示所有其他文件:

I have a site under development with a "News" section and an "Older News" section. The news are stored individually in external PHP files which are loaded by the server by PHP (the server counts the number of PHP files and displays the last one – the current news – in the "News" sections and all the others in the "Older News" section:

<!-- ####### NEWS ####### -->

<div id="news">
    <h2>NEWS</h2>

    <div>
        <a id="showoldnews" href="#news">OLDER</a>
    </div>

    <?php $directory = "assets/news/"; if (glob($directory . "*.php") != false) { $newscount = count(glob($directory . "*.php")); } else {} ?>

    <?php include('assets/news/news' . $newscount . '.php'); ?>

    <!-- ####### OLDER NEWS ####### -->

    <div id="oldnews">
        <h2>OLDER NEWS</h2>

        <?php
            $newscount = $newscount-1;
            while ($newscount>0) {
                include('assets/news/news' . $newscount .'.php');
                --$newscount;
            }
        ?>
    </div>

较旧的新闻"部分最初是隐藏的,仅由jQuery触发器显示:

The "Older News" section is initially hidden and only made visible by a jQuery trigger:

// Show old NEWS
$('a#showoldnews').click(function () {
    $('#oldnews').show(400);
});

但是我希望长期来看会出现问题:由于所有新闻都是从头开始加载的,因此这意味着随着越来越多的新闻发布,网站的加载速度会变慢.即使它是可接受的",它仍然不是理想的解决方案.

But I am expecting problems in long-term: since all news are loaded in the first place, this means that with more and more news coming up the website will be loading slower. And even if it's "acceptable", it's still not the ideal solution.

我在考虑使用jQuery.load()加载外部PHP文件,然后仅在用户要求时才加载旧新闻.

I was thinking on using jQuery.load() to load an external PHP file that would then load the old news only when the user asks for it.

问题是我不知道在用户单击链接后是否可以通过使用jQuery.load()加载以下脚本:

The problem is that I don't know if its possible to load the following script by using jQuery.load() after the user clicks on a link:

<?php
    $directory = "assets/news/";
    if (glob($directory . "*.php") != false) {
        $newscount = count(glob($directory . "*.php"));
    }
    else {
    }
?>
<?php
    include('assets/news/news' . $newscount . '.php');
?>

这种方法可以接受吗?我已经尝试过,但是根本没有用(我可以用load()函数加载静态内容,但是我无法使服务器处理PHP脚本.我不知道是否有可能使服务器处理PHP代码,因为PHP处理是在网站开始加载之前在服务器端进行的.

It this approach acceptable? I have tried, but it didn't work at all (I could load static content with the load() function, but I was not able to make the server process the PHP script. I don't know if it's possible to make the server process the PHP code, because PHP processing is done on the the server side before the website begins loading... which is not the case.

更新#1

我具有从服务器加载"load.php"文件的以下内容:

I have the following that loads the 'load.php' file from the server:

<script type="text/javascript">
    $('#oldnews').load('load.php');
</script>

load.php文件的内容为:

The content of the load.php file is:

<div class="section" id="oldnews">
    <h2>OLDER NEWS</h2>

    <div class="topLink">
        <a href="#">TOP</a>
    </div>

    <div class="topLink2">
        <a id="hideoldnews" href="#news">HIDE</a>
    </div>

    <?php
        $newscount = $newscount-1;
        while ($newscount>0) {
            include('assets/news/news' . $newscount .'.php');
            --$newscount;
        }
    ?>
</div>

出现"TOP"和"HIDE"链接没有任何问题.但是,PHP块似乎根本没有被处理...

The "TOP" and "HIDE" links appear without any problems. However, the PHP block seems to not be processed at all...

推荐答案

PHP 可以在jQuery抓取内容之前运行...这是我设置和测试的...

PHP can be run before jQuery grabs the content... Here is what I setup and tested...

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/JavaScript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    </head>

    <body>
        <div id="feeds"><b>45</b> feeds found.</div>
        <script type="text/JavaScript">
            $("#feeds").load("go.php");
        </script>
    </body>
</html>

go.php

<?php
    echo 'Hi';
?>

结果是#feeds .innerHTML为嗨" ...在复制情况时,我希望获得更多信息/代码.窥视 jQuery加载API文档.

And the result is that #feeds .innerHTML is "Hi"... I would love more information/code in replicating the situation. It might help to take a peek at the jQuery load API documentation.

这篇关于使用jQuery的'load()'加载带有内部PHP include函数的外部PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆