2012-02-13 80 views
2

在过去几天我漫步在网络上的脚本,将有我希望的元素..最好的因此是这一个http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp,但有很多问题,我需要更好的解决方案:加载动态PHP和MySQL页面无需刷新 - AJAX

  • 我不想要标签:我想要所有链接加载页面像文件夹(www.site.com/first/site/)。这在github上很神奇(点击这里https://github.com/spjoe/deluge-yarss-plugin,然后点击一个随机文件,然后回头看看地址栏)。如果有人知道他们在做什么或如何做,这将非常酷。

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

  • 史:我想有在网站上的选项,其中向后和向前运动将完美与动画工作,因为它的工作原理,同时点击链接..

  • 多变的div里面的工作联系:因为有是一些脚本不会改变内容链接的内容的问题(例如第一个内容框包括导航,应该用第二个框中的不同导航来替换),再次使用github这个工作。

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

+0

更好地为您遇到的特定问题撰写具体问题。 – dynamic 2012-02-13 15:14:16

回答

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')),仅渲染应替换<section id="content">内容的部分,否则渲染整个页面。 Pjax足够智能,如果它工作,它将只取代<section id="content">的内容,如果它不起作用,你仍然有正常的链接工作。如果你使用的话,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 
} 
?> 
+0

伟大的思想,呃? – JKirchartz 2012-02-13 15:17:55

+0

确实:) 它完全符合他的问题.. – ramsesoriginal 2012-02-13 15:29:03

+0

它是一个伟大的脚本,即使有IE兼容性的一些问题,..但是,我不喜欢关于pjax的是,它基于Ruby和我想在PHP中有一些东西.. – 2012-02-14 14:52:35

0

如果你想要的网址,当你阿贾克斯页面请求,您可以使用显示的“正确”的网址PJAX它改变了历史与pushState但它不工作ie6-ie9检查出CanIUse: History兼容性问题。

相关问题