【问题标题】:Passing Variable to other PHP without refreshing将变量传递给其他 PHP 而不刷新
【发布时间】:2016-11-27 03:20:22
【问题描述】:

我是 PHP 新手。我有这 3 个文件:

  • index.php
  • functions.php(用于组织函数)
  • header.php

我想简化(到目前为止已经完成)index.php 页面,因此我不需要一次又一次地编写所有内容。所以我创建了 header.php 可以被 index.php 加载:

header.php

<!Doctype html>
<html lang="en">

<head>
    <title>Learn PHP</title>  <!--This is the problem, every page that loads header.php will have same title! -->
</head>

<body>
<div class="header">

    <h1>Learn PHP</h1>
    <p>Your library of PHP learning!</p>
    <hr/>
</div>
<!-- footer is handled by footer.php that include </body> and </html>-->

我什至通过在 functions.php 中创建一个函数来进一步简化事情,这样我就可以在 index.php 中键入“get_header()”而无需编写再次完整的代码。

functions.php

    <?php

function get_header(){
        if (file_exists('header.php')){
                require 'header.php';
            }
            else{
                echo "There is an error retrieving a file";
            }   
    }

?>

现在,我如何允许这个 index.php 有自定义页面标题,而不是 header.php 给出的默认值?

我是否错过了一些重要的事情。我尝试创建一个变量并尝试将其传递给functions.php,但它不起作用。或者有什么更清洁的方法吗?

wordpress 如何组织他们的文件让我受到启发,我检查了 wordpress 文件。然后我决定从头开始尝试一些东西,以便我更好地理解并提高我的 PHP 技能。

我知道可以使用 POST 和 GET,但不,我不想刷新或加载新页面只是为了更改页面标题,尤其是 index.php

编辑:

这里我包含了我的 index.php

<?php
    require 'functions.php';
?>
<?php 
    get_header();
?>  

<table>
    <h3>What we learned</h3>

    <ul>
        <li><a href="sample">Syntax</a>         </li>
        <li><a href="sample">Variables</a>      </li>
        <li><a href="sample">Code Flow</a>      </li>
        <li><a href="sample">Arrays</a>         </li>
        <li><a href="sample">Superglobals</a>   </li>

    </ul>
</table>

<?php get_footer(); ?>

【问题讨论】:

  • 使用ajax传递变量而不刷新页面
  • 我知道你的意思,但这发生在页面加载之前。不需要 AJAX,除非我要求用户点击某些东西,这是不需要的,因为我尝试从服务器端自动执行此操作。

标签: php


【解决方案1】:

似乎您只需要简单的包含即可。实际上,在这里使用函数会使它变得更难,因为包含的范围与包含它的范围相同。例如

header.inc

…
<title><?php echo isset($title) ? $title : 'Untitled';?></title>
…

index.php

<?php
$title = 'Welcome';
require 'header.inc';
?>
welcome

另一个页面.php

<?php
$title = '2nd page';
require 'header.inc';
?>
2nd page content

如果你想使用一个函数,给它参数。

function get_header($title = 'Some default title') {
    …
}

包含的文件将可以访问函数范围内的变量。

【讨论】:

    【解决方案2】:

    在functions.php中

    function get_header(){
        if (file_exists('header.php'))
          require 'header.php';
        else echo "There is an error retrieving a file";   
    }
    

    在 header.php 中,在应答器标题中调用会话参数

    <!Doctype html>
    <html lang="en">
    
    <head>
        <title>
         <?php if(!empty($_SESSION['title-page'])) echo $_SESSION['title-page']; else  'Learn PHP'; ?>
        </title>
    </head>
    
    <body>
    <div class="header">
    
        <h1>Learn PHP</h1>
        <p>Your library of PHP learning!</p>
        <hr/>
    </div>
    <!-- footer is handled by footer.php that include </body> and </html>-->
    

    在 index.php 中

    <?php 
        session_start(); 
        $_SESSION['title-page'] = 'this is the welcome Page'; 
        require 'functions.php'; 
        get_header(); 
    ?>
    
    <table>
        <h3>What we learned</h3>
    
        <ul>
            <li><a href="sample">Syntax</a>         </li>
            <li><a href="sample">Variables</a>      </li>
            <li><a href="sample">Code Flow</a>      </li>
            <li><a href="sample">Arrays</a>         </li>
            <li><a href="sample">Superglobals</a>   </li>
        </ul>
    </table>
    
    <?php get_footer(); ?>
    

    在另一个page.php中

    <?php 
        session_start(); 
        $_SESSION['title-page'] = 'this is an another Page'; 
        require 'functions.php'; 
        get_header(); 
    ?>
    
    <table>
        <h3>What we learned</h3>
    
        <ul>
            <li><a href="sample">Syntax</a>         </li>
            <li><a href="sample">Variables</a>      </li>
            <li><a href="sample">Code Flow</a>      </li>
            <li><a href="sample">Arrays</a>         </li>
            <li><a href="sample">Superglobals</a>   </li>
        </ul>
    </table>
    
    <?php get_footer(); ?>
    

    【讨论】:

    • 很抱歉,我不太明白这是如何工作的。此外,header.php 也被 index.php 加载。 index.php > header.php > 函数.php
    • 不,它没有,它抛出一个错误 require(header.php?title=title): failed to open stream。我想我错过了一些东西,但我也确实把参数放在了 get_header("title")
    • @Mohd.Zafranudin,结果如何?
    • 是的,它可以工作,这就是我想要实现的逻辑,通过查看代码我明白了很多。谢谢你。 session_start() 现在对我有意义
    • 这不是会话变量的用途。它们用于在单独的页面加载之间保留变量,而不是在单个页面加载中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多