2016-07-23 48 views
2

我是PHP新手。我有這3個文件:將變量傳遞給其他PHP而不刷新

  • 的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(); ?> 
+0

使用ajax傳遞變量而不刷新頁面 –

+0

我知道你的意思,但是這發生在頁面甚至加載之前。除非我要求用戶點擊某個東西,否則不需要AJAX,因爲我試圖從服務器端自動執行此操作。 –

回答

0
在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(); ?> 
+0

對不起,但我不太明白這是如何工作的。另外,header.php也被index.php加載。 index.php> header.php> functions.php –

+0

@ Mohd.Zafranudin;它適合你嗎?或不是 – bfahmi

+0

不,它引發錯誤require(header.php?title = title):未能打開流。我想我錯過了一些東西,但我確實把參數放在了get_header(「title」)上 –

1

看起來你所需要的只是簡單的包括。在這裏,通過使用函數實際上讓它變得更加困難,因爲包含的範圍與包含的範圍相同。例如。

header.inc

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

的index.php

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

另一-page.php文件

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

如果你想使用的功能,給它的參數。

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

包含的文件將有權訪問函數範圍內的變量。

相關問題