【问题标题】:Loading dynamic php&mysql pages without refreshing - AJAX加载动态 php&mysql 页面而不刷新 - AJAX
【发布时间】:2012-03-04 23:46:09
【问题描述】:

在过去的几天里,我在网上四处游荡,寻找一个包含我想要的元素的脚本。到目前为止最好的是这个http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp,但是有很多问题,我需要更好的解决方案:

  • 我不想要标签:我想要所有用于加载页面的链接,例如文件夹 (www.site.com/first/site/)。这在 github 上很神奇(单击此处https://github.com/spjoe/deluge-yarss-plugin,然后单击随机文件并在观看地址栏时返回)。如果有人知道他们在做什么或如何做,那就太好了。

  • 我需要动态内容:上面的脚本只从 switch 函数加载“案例”,而我需要 php 检查 url 并在 mysql 数据库中查找文件并根据 url 从所需表中回显内容,如果在数据库中找到它(如果我指向 site.com/harvey 的链接,php 应该在数据库中找到 Harvey 并显示例如他的电话、地址和其他详细信息,而这不能预先加载,但只能请求由于带宽问题而导致点击)。

  • 历史:我希望有一个选项,使网站上的前后移动可以完美运行,并且在单击链接时动画可以正常运行。..

  • 可变 div 中的工作链接:因为某些脚本存在问题,它们不会更改链接所在的内容(例如,第一个内容框包含导航,应将其替换为第二个框),这同样适用于 github..

我希望你给我一些链接,甚至更多的代码帮助..

【问题讨论】:

  • 最好为您遇到的特定问题写一个特定的问题。

标签: php jquery mysql ajax refresh


【解决方案1】:

我认为您正在寻找的脚本是pjax,它具有您想要的所有功能。你可以在github上找到:https://github.com/defunkt/jquery-pjax/tree/heroku

编辑

您可以将它与您喜欢的任何服务器端语言一起使用。例如,

<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>

<script src="jquery.pjax.js"></script>
<script type="text/javascript">
    $(function(){
      $('nav a').pjax('#content')
    })
</script>

在您网站的脑海中,

<nav>
  <a href="otherContent1.php" >click me for other content</a>
  <a href="otherContent2.php" >click me for even more additional content</a>
</nav>
<section id="content">
  <h1>Original Content</h2>
  <p>
    this will be replaced by pjax with the content's of <a href="otherContent1.php">otherContent1.php</a> or <a href="otherContent2.php">otherContent2.php</a> 
  </p>
</section>

在您的主模板中并修改您的 php 代码,使其查找 HTTP_X_PJAX 标头。如果它存在(参见xhr.setRequestHeader('X-PJAX', 'true'))只渲染应该替换&lt;section id="content"&gt; 内容的部分,否则渲染整个页面。 Pjax 足够智能,如果它工作,它只会替换&lt;section id="content"&gt; 的内容,如果它不工作,你仍然有正常的链接工作。 pjax 甚至会处理谷歌分析,如果你正在使用它,那么你的跟踪仍然有效。

EDIT2:示例

好的,这里有一个示例 php 页面。请注意,我没有对此进行了测试,我只是在堆栈溢出时快速而肮脏地写了它,以展示您如何解决这个问题。此代码未经测试,当然还没有准备好生产。但作为一个例子,你可以做这样的事情(文件应该命名为 index.php):

<?php
$render_template=true;
if ($_SERVER["HTTP_X_PJAX"])
{
    $render_template=false;
}
?>

<?php
if ($render_template) {
?>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Testing Pjax</title>
        <script src="jquery.js"></script>
        <script src="jquery.cookie.js"></script>

        <script src="jquery.pjax.js"></script>
        <script type="text/javascript">
            $(function(){
              $('nav a').pjax('#content')
            })
        </script>
    </head>
    <body>
        Date in template: <?php echo $today = date("D M j G:i:s T Y"); ?>
        <nav>
            <a href="index.php?content=1" >click me for other content</a>
            <a href="index.php?content=2" >click me for even more additional content</a>
        </nav>
        <section id="content">
<?php 
}
?>
<?php
if ($_Get["content"]==1) 
{
?>

            <h1>Content=1</h1>
            Date in content: <?php echo $today = date("D M j G:i:s T Y"); ?>
            <p>
                this will be replaced by pjax with the content's of <a href="index.php?content=1">index.php?content=1</a> or <a href="index.php?content=2">index.php?content=2</a> 
            </p>
<?php 
} else {
?>
            <h1>Content 2=2</h1>
            Date in content: <?php echo $today = date("D M j G:i:s T Y"); ?>
            <p>
                this will be replaced by pjax with the content's of <a href="index.php?content=1">index.php?content=1</a> or <a href="index.php?content=2">index.php?content=2</a> 
            </p>
<?php 
}
?>
<?php
if ($render_template) {
?>
        </section>
    </body>
</html>
<?php 
}
?>

【讨论】:

  • 这是一个很棒的脚本,即使在 IE 兼容性方面存在一些问题,.. 但是,我不喜欢 pjax 的地方是它基于 Ruby,我想要一些东西在 PHP 中..
  • 这不正确,它是一个完全独立于服务器端语言的javascript。我用一些额外的信息更新了答案,所以你可以看看如何让它发挥作用。
  • 您好!我认为最好将此答案标记为已接受,因为它可以解决您的问题,并且只需用您关于动画的问题(可能链接到此问题)创建一个新答案,从那时起您将获得越来越多有价值的答案。
  • 我想问一下如何添加代码来查找 http_X_pjax 标头,因为没有那个脚本就无法工作。我在标头中随着时间的推移对其进行测试,并且只有内容才会改变应该更新..
  • 我添加了一个 sample index.php。我没有测试它是否有效,只是为了向您展示原则上如何做到这一点。显然,您必须根据自己的需要对其进行调整。
【解决方案2】:

如果您希望 URL 在 ajax 页面请求时显示“正确”的 url,您可以使用 PJAX 它会使用 pushState 更改历史记录,但它不适用于 ie6-ie9,请查看 CanIUse: History兼容性问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    • 2020-06-25
    相关资源
    最近更新 更多