
make_page() 需要兩個參數 (parameter) $main 與 $display ,其中建立一個字串 (string) ,最後回傳這個字串
function make_page( $main , $display ) { $page = " <html> <head> <title>Encode Software</title> <link rel= 'stylesheet' href= 'encryptor.css' > </head> <body> <div class = 'menu' > <a href= '{$_SERVER[' PHP_SELF ']}' >Home</a> - <a href= '{$_SERVER[' PHP_SELF ']}?op=record' >Record</a> - <a href= '{$_SERVER[' PHP_SELF ']}?op=about' >About</a> </div> <div class = 'main' > $main </div> <div class = 'display' > $display </div> </body> </html>"; return $page ; } |
第一個參數 $main 依不同選單提供第二個區塊的內容,第二個參數 $display 依按鈕及選單顯示結果與提示訊息。
這裡須留意一點,選單網址原本用 echo
<a href= "<?PHP echo $_SERVER['PHP_SELF']; ?>" >Home</a> - <a href= "<?PHP echo $_SERVER['PHP_SELF'].'?op=record'; ?>" >Record</a> - <a href= "<?PHP echo $_SERVER['PHP_SELF'].'?op=about'; ?>" >About</a> |
這裡已經改成大括弧了
<a href= '{$_SERVER[' PHP_SELF ']}' >Home</a> - <a href= '{$_SERVER[' PHP_SELF ']}?op=record' >Record</a> - <a href= '{$_SERVER[' PHP_SELF ']}?op=about' >About</a> |
因為寫在 HTML 語法裡,必須用 echo 使變數變成字串輸出,而 PHP 字串裡用大括弧即可。
在編碼、解碼頁,也就是選單的 Home 頁, $main 的內容由 input_form() 提供。 input_form() 如下
function input_form() { $main = " <form action= '{$_SERVER[' PHP_SELF ']}' method= 'post' > <input type= 'text' name= 'input' size= '67' ><br / > <input type= 'submit' value= 'New' name= 'op' > <input type= 'submit' value= 'Load' name= 'op' > <input type= 'submit' value= 'Encode' name= 'op' > <input type= 'submit' value= 'Decode' name= 'op' > <input type= 'submit' value= 'Clean' name= 'op' > </form>"; return $main ; } |
同樣的, input_form() 建立字串也回傳所建立的字串。
我們把 make_page() 與 input_form() 放在整理函數的 encryptor_function.php 裡, include 進來就可以用囉!
完整程式檔案請參考 encryptor_function.php 。
這樣一來,我們把 encryptor02.php 修改成只有 switch 加 make_page()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | <?php // include include "encryptor_function.php" ; // switch switch ( $_REQUEST [ "op" ]) { case "New" : $main = input_form(); $display = "There is <b>New</b> button." ; break ; case "Load" : $main = input_form(); $display = "There is <b>Load</b> button." ; break ; case "Encode" : $main = input_form(); $display = "There is <b>Encode</b> button. Your input is '{$_POST['input']}'." ; break ; case "Decode" : $main = input_form(); $display = "There is <b>Decode</b> button. Your input is '{$_POST['input']}'." ; break ; case "Clean" : $main = input_form(); $display = "There is <b>Clean</b> button." ; break ; case "record" : $main = "This is record page.." ; $display = "This is record page.." ; break ; case "about" : $main = "This is about page.." ; $display = "This is about page.." ; break ; default : $main = input_form(); $display = "something happened" ; } // make page echo make_page( $main , $display ); /* 《程式語言教學誌》的範例程式 檔名:encryptor03.php 功能:示範 PHP 程式 作者:張凱慶 時間:西元 2012 年 11 月 */ ?> |
結果如下

接下來要開始整合建立 Encrypt 物件 (object) 的功能了,使用者按下 New 後伺服器雖然新增一個 Encrypt ,可是只存在於伺服端的那一下子,等到 Encode 的時候,剛才新增的 Encrypt 卻不見了說。這個問題在於客戶端與伺服端是分開執行的,因此我們要先學會儲存 Encrypt 物件。
中英文術語對照 | |
---|---|
函數 | function |
參數 | parameter |
字串 | string |
物件 | object |
您可以繼續參考
網站篇
相關目錄
回 PHP 入門指南
回 PHP 教材
回首頁
參考資料
http://www.php.net/manual/en/tutorial.firstpage.php
http://www.php.net/manual/en/tutorial.useful.php
http://www.php.net/manual/en/functions.user-defined.php
http://www.php.net/manual/en/functions.arguments.php
http://www.php.net/manual/en/functions.returning-values.php
沒有留言:
張貼留言