我是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(); ?>
使用ajax傳遞變量而不刷新頁面 –
我知道你的意思,但是這發生在頁面甚至加載之前。除非我要求用戶點擊某個東西,否則不需要AJAX,因爲我試圖從服務器端自動執行此操作。 –