jQuery的pjax用PHP。在重装没有内容重装、内容、jQuery、pjax

2023-09-10 19:43:46 作者:不流泪的眼//

我尝试使用jQuery pjax加载我的内容股利。这工作正常,但如果我重新加载页面我没有的内容。我想我需要把页面内容在其他{一部分,但我并不想集全HTML的PHP​​变量。如何解决这个问题?

的index.php:

 < PHP
    $标题='家';
    如果(使用isset($ _ SERVER ['HTTP_X_PJAX'])及和放大器; $ _ SERVER ['HTTP_X_PJAX'] =='真'){&GT?;
        < D​​IV ID =内容>
            < H1>家< / H1>
        < / DIV>
    <?PHP的回声<冠军> {$标题}< /标题>中;
    }其他{
        包括wrapper.php;
    }
?>
 

wrapper.php:

 < UL类=NAV导航栏 - 导航的风格=保证金左:20像素;>
    <李>< A HREF =的index.php'数据pjax ='内容'>首页< / A>< /李>
    <李>< A HREF =page1.php中数据pjax ='内容'>演示1< / A>< /李>
    <李>< A HREF =page2.php中将数据pjax ='内容'>演示2'; / A>< /李>
< / UL>
< D​​IV ID =内容>< / DIV>
 

在JS:

  $(文件)。就绪(函数(){
    $(文件).pjax('A [数据pjax]','#内容,{
        片段:#内容
    });
});
 
JQuery

page1.php中

 <如果PHP(使用isset($ _ SERVER ['HTTP_X_PJAX'])及和放大器; $ _ SERVER ['HTTP_X_PJAX'] =='真'){&GT?;
    < D​​IV ID =内容>
        < H1>页面1 LT; / H1>
    < / DIV>
< PHP)其他(包括wrapper.php';}>
 

page2.php中将

 <如果PHP(使用isset($ _ SERVER ['HTTP_X_PJAX'])及和放大器; $ _ SERVER ['HTTP_X_PJAX'] =='真'){
?>
    < D​​IV ID =内容>
        < H1>页面2'; / H1>
    < / DIV>
< PHP}其他{
    包括wrapper.php;
}
?>
 

解决方案

您只包括什么时候被请求的页面不PJAX的wrapper.php(==正常的方式)。在wrapper.php内容是空的。

在wrapper.php你应该包括正确的网页,如果请求源是不PJAX:

首先,你应该重新命名了一些文件。使用的index.php为主体的布局设计文​​件(你打算如何处理包装)。添加在p作为查询参数(包括可读取在PHP真正的 $ _ GET )点击链接时加载网页。

创建一个文件home.php这将是在的index.php在#内容加载默认的页面。

的index.php

 < UL类=NAV导航栏 - 导航的风格=保证金左:20像素;>
    <李>< A HREF =的index.php'数据pjax ='内容'>首页< / A>< /李>
    <李><'?的index.php P = 1'A HREF =数据pjax ='内容'>演示1< / A>< /李>
    <李><'?的index.php P = 2'A HREF =数据pjax ='内容'>演示2'; / A>< /李>
< / UL>
< D​​IV ID =内容>
    <如果PHP(!使用isset($ _ SERVER ['HTTP_X_AJAX'])){
        如果(!使用isset($ _ GET ['P'])){
            包括('home.php');
        }
        其他 {
            包括(页面$ _ GET ['P']。PHP。');
        }
    ?}>
< / DIV>
 

这是非常简单的例子。在现场应用中,这应该以另一种方式,以防止某些攻击的脚本来完成。

我猜你是首发出场。请看看codeIgniter。它是一个框架,它有许多现成的使用功能。它已被证明是容易学习...

I try to use jquery pjax to load my contents in a div. this works fine, but if i reload the page i have no content. i think i need to put the page content in the else{ part, but i don't want to set the whole html in a php variable. how to fix this?

index.php:

<?php
    $title = 'Home';
    if(isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX'] == 'true'){ ?>
        <div id="content">
            <h1>home</h1>
        </div>
    <?php echo "<title>{$title}</title>";
    }else{
        include 'wrapper.php';
    }
?>

wrapper.php:

<ul class="nav navbar-nav" style='margin-left:20px;'>
    <li><a href='index.php' data-pjax='content'>Home</a></li>
    <li><a href='page1.php' data-pjax='content'>Demo 1</a></li>
    <li><a href='page2.php' data-pjax='content'>Demo 2</a></li>
</ul>
<div id="content"></div>

The JS:

$(document).ready(function() {
    $(document).pjax('a[data-pjax]', '#content', { 
        fragment: '#content' 
    });
});

page1.php

<?php if(isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX'] == 'true'){  ?>
    <div id="content">
        <h1>Page 1</h1>
    </div>
<?php } else{ include 'wrapper.php';} ?> 

page2.php

<?php if(isset($_SERVER['HTTP_X_PJAX']) && $_SERVER['HTTP_X_PJAX'] == 'true'){
?>
    <div id="content">
        <h1>Page 2</h1>
    </div>
<?php }else{
    include 'wrapper.php';
}
?>

解决方案

You are only including the wrapper.php when the page is requested without PJAX (== the normal way). In wrapper.php the content is empty.

In wrapper.php you should include the correct page if the request origin is not PJAX:

First of all you should rename some files. Use index.php as the main lay-out file (what you intended to do with wrapper). Add the p as query param (which can be read true $_GET in PHP) to load pages when clicking links.

Create a file home.php which will be the default page loaded in #content at index.php.

index.php

<ul class="nav navbar-nav" style='margin-left:20px;'>
    <li><a href='index.php' data-pjax='content'>Home</a></li>
    <li><a href='index.php?p=1' data-pjax='content'>Demo 1</a></li>
    <li><a href='index.php?p=2' data-pjax='content'>Demo 2</a></li>
</ul>
<div id="content">
    <?php if(!isset($_SERVER['HTTP_X_AJAX'])) {
        if(!isset($_GET['p'])) {
            include('home.php');
        }
        else {
            include('page'.$_GET['p'].'.php');
        }
    } ?>
</div>

This is very simple example. In live applications this should be done in another way to protect the script against certain attacks.

I'm guessing you are a starter. Please take a look at CodeIgniter. It is a framework which has a lot of ready-to-use functions. It has proven to be 'easy' to learn...