iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 27
1
Modern Web

~網頁入門~系列 第 27

Day27 – PHP – HTTP Method (Get & Post)

在 HTTP 中有很多種 Method(方法),而在製作表單時,用 method 屬性來指定提交表單數據的方法,有 GetPost 兩種。

Get 和 Post 有什麼區別呢?

以寄信的例子來說,Get 就像是明信片,直接把要傳遞的資訊寫在上面,將要傳送的資料加在要寄送的地址(URL)上;
而 Post 則是一般信件,將要傳遞的資訊寫在信紙上,裝進信封袋裡,然後將要寄送的地址(URL)寫在信封上。

接下來用範例來更加了解他們怎麼運作吧!

Get

在網址後面加入 ?key1=value1&key2=value
? 和 URL 連接,& 串聯資料
https://ithelp.ithome.com.tw/upload/images/20191013/20120959SNfoyZ52mQ.jpg
圖例中提供了 id 和 str 兩個資料。

會以陣列的方式儲存

<?php
    print_r($_GET);
?>

https://ithelp.ithome.com.tw/upload/images/20191013/20120959L3mhPsY4mz.jpg

試著用索引叫出陣列的值
https://ithelp.ithome.com.tw/upload/images/20191013/20120959QDehOucH35.jpg

https://ithelp.ithome.com.tw/upload/images/20191013/20120959fWKVM8SruV.jpg

https://ithelp.ithome.com.tw/upload/images/20191013/20120959LesKaZZqL0.jpg

https://ithelp.ithome.com.tw/upload/images/20191013/20120959DYgJqzJHdE.jpg


Post

以表單為例,action 屬性中指定本地位址 post.php
https://ithelp.ithome.com.tw/upload/images/20191013/201209594uUSCCUtrN.jpg

https://ithelp.ithome.com.tw/upload/images/20191013/20120959xoPutroMTY.jpg

用索引叫出輸入成績的值,並試著使用運算子計算總分和平均
https://ithelp.ithome.com.tw/upload/images/20191013/20120959shzQbMtciS.jpg

https://ithelp.ithome.com.tw/upload/images/20191013/20120959O0vS2UF2y0.jpg

點擊送出表單後,會將填寫內容包起來送到 action 指定的位址,並用陣列儲存
https://ithelp.ithome.com.tw/upload/images/20191013/20120959E1W1Ym3RGl.jpg

資料不會加在URL的後面

上一篇
Day26 – PHP – 陣列
下一篇
Day28 – PHP – 流程控制
系列文
~網頁入門~30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言