PHP語言提供系統內定的陣列處理來自「網頁伺服器、網頁執行環境、使用者在瀏覽器上輸入的資料」,陣列變數如下所述:
$_GET:經由網頁表單以 get 方式傳遞產生。
$_POST:經由網頁表單以 post 方式傳遞產生。
$_COOKIE:網頁傳遞 Cookie 資料。
$_SESSION:網頁傳遞 Session 資料。
$_FILES:經由網頁表單傳遞檔案方式產生。
$_SERVER:由網頁伺服器設定產生。
<form action="http://www.google.com/search" method="get">
<h1><b style="color: red;">製作搜尋的輸入欄位</b><br/>應用Google 搜尋功能</h1>
<p><input type="search" name="q" autofocus placeholder="請輸入關鍵字"></p>
<!-- <p><input type="submit" value="搜尋"></p> -->
<!-- 製作圖片按鈕 image 屬性 -->
<p><input src="cf.png" type="image" alt="Search"></p>
</form>
<form action="cf.php" method="get">
<h1>製作選單按鈕 radio 屬性</h1>
<p>
姓名:<input type="text" name="user">
</p>
<p>
<input type="radio" name="sex" value="男性">男性<br>
<input type="radio" name="sex" value="女性">女性
</p>
<p>
<input type="radio" name="age" value="20歲以下">20歲以下<br>
<input type="radio" name="age" value="20-30歲" checked>20-30歲<br>
<input type="radio" name="age" value="30-40歲">30-40歲<br>
<input type="radio" name="age" value="40-50歲">40-50歲<br>
<input type="radio" name="age" value="50-60歲">50-60歲<br>
<input type="radio" name="age" value="60歲以上">60歲以上<br>
</p>
<p><input type="reset" value="取消"> <input type="submit" value="確認"></p>
</form>
<?php
$user = $_GET['user'];
$sex = $_GET['sex'];
$age = $_GET['age'];
?>
<div style="font-weight:bold; color:red;">應用 <span style="color:blue;">$_GET[' ']</span> 取值的範例</div><div> </div>
<div>
<?php echo $user." ".$sex." ".$age; ?>
</div>
<form action="cf.php" method="post">
<h1>製作 特定範圍 range 屬性</h1>
<p>姓名: <input type="text" name="user" autofocus></p>
<p>滿意度調查分數: <input type="range" name="score" min="0" max="100" step="1" value="60"></p>
<p><input type="reset" value="取消"> <input type="submit" value="確認"></p>
</form>
<?php
$user = $_POST['user']; $score = $_POST['score'];
?>
<div style="font-weight:bold; color:red;">
應用 <span style="color:blue;">$_POST[' ']</span>
取值的範例</div><div>
</div>
<div>
<?php
echo $user." "."<br/>滿意度調查分數: ".$score." 分";
?>
</div>
<form action="cf.php" method="post">
<h1>製作核取方塊 checkbox 屬性</h1>
<p>姓名: <input type="text" name="user" autofocus></p>
<p>性別:<br>
<input type="radio" name="sex" value="男性" checked>男性<br>
<input type="radio" name="sex" value="女性">女性<br>
</p>
<p>年齡:<br>
<!-- 數值輸入按鈕屬性 -->
<input type="number" name="num" min="1">
</p>
<p>熟悉的程式語言:<br>
<input type="checkbox" name="language[]" value="HTML5">HTML5<br>
<input type="checkbox" name="language[]" value="PHP">PHP<br>
<input type="checkbox" name="language[]" value="JavaScript">JavaScript<br>
<input type="checkbox" name="language[]" value="JQuery">JQuery<br>
</p>
<p><input type="reset" value="取消"> <input type="submit" value="確認"></p>
</form>
<?php
$user = $_POST['user'];
$sex = $_POST['sex'];
$num = $_POST['num'];
?>
<div style="font-weight:bold; color:red;">應用 <span style="color:blue;">$_POST[' ']</span> 取值的範例</div><div> </div>
<div>
<?php
echo $user." ".$sex." 年齡: ".$num."歲<br/>熟悉的程式語言:<br/>";
foreach ($_POST['language'] as $value) {
echo $value."<br>";
}
?>
</div>
<h1>一個檔案上傳範例教學</h1>
<form method="post" action="cf.php" enctype="Multipart/Form-data">
<table border="1">
<tr>
<td align="center" width="50%"></td><td align="center"></td>
</tr>
<tr>
<td align="center" colspan="2" style="color: green;"><b>檔案上傳作業</b></td>
</tr>
<tr>
<td align="center"><mark>不限定檔案格式<mark></td><td><input type="file" name="upfile" /></td>
</tr>
<tr>
<td align="center"><input type="submit" value="上傳檔案" /></td><td align="center"><input type ="reset" value="清除"></td>
</tr>
</table>
</form>
<?php
header("Content-Type:text/html; charset=utf-8");
echo "<title>訊息通知</title>";
if (!empty($_FILES["upfile"]["tmp_name"])){
// echo "暫存名稱: ".$_FILES["upfile"]["tmp_name"];
move_uploaded_file($_FILES["upfile"]["tmp_name"],"upload/".$_FILES["upfile"]["name"]);
echo "一個檔案===>上傳檔案成功!<br/><br/>";
}else{
echo "沒有個檔案===>上傳檔案失敗!<br/><br/>";
}
echo "<a href='javascript:history.go(-1)'>返回首頁</a>";
?>
$rename = $_FILES["upfile"]["name"];
$rename = "rename".substr($rename, -4);
move_uploaded_file($_FILES["upfile"]["tmp_name"],"upload/".$rename);
<h1>2個檔案上傳+限定檔案範例教學</h1>
<form method="post" action="cf.php" enctype="Multipart/Form-data">
<table border="1">
<tr>
<td align="center"></td><td align="center"></td>
</tr>
<tr>
<td align="center">下載 xlsx 檔案</td><td align="center">1. <a href="upload/hhh.xlsx">HHH</a> 2. <a href="upload/fff.xlsx">FFF</a></td>
</tr>
<tr>
<td align="center" colspan="2" style="color: red;">限定 hhh.xlsx, fff.xlsx 檔案上傳</td>
</tr>
<tr>
<td align="center">選擇檔案<br/></td><td><input type="file" name="upfile[]" /><br/><input type="file" name="upfile[]" /></td>
</tr>
<tr>
<td align="center" colspan="2"><input type="submit" value="上傳檔案" /></td>
</tr></table>
</form>
<?php
header("Content-Type:text/html; charset=utf-8");
echo "<title>訊息通知</title>";
for ($i=0;$i<(count($_FILES["upfile"])-3);$i++){
if ($_FILES["upfile"]["error"][$i] > 0){
echo "錯誤訊息:第 ".($i+1)." 個上傳檔案空白...<br/>";
echo "<script>alert('錯誤訊息:第 '+($i+1)+' 個上傳檔案空白...')</script>";
}else{
if ((substr(trim($_FILES["upfile"]["name"][$i]),0,8) <> "hhh.xlsx") && (substr(trim($_FILES["upfile"]["name"][$i]),0,8) <> "fff.xlsx")){
echo "第".($i+1)."個檔案名稱: ".$_FILES["upfile"]["name"][$i]."<br/>";
echo "第 ".($i+1)." 個檔案===>上傳檔案失敗!<br/><br/>";
echo "<script>alert('錯誤訊息:第 '+($i+1)+' 個上傳檔案之檔名錯誤,請選擇檔案 hhh.xlsx 或 fff.xlsx,謝謝!')</script>";
}else{
echo "第".($i+1)."個檔案名稱: ".$_FILES["upfile"]["name"][$i]."<br/>";
echo "檔案類型: ".$_FILES["upfile"]["type"][$i]."<br/>";
// echo "暫存名稱: ".$_FILES["upfile"]["tmp_name"][$i]."<br/>";
move_uploaded_file($_FILES["upfile"]["tmp_name"][$i],"upload/".$_FILES["upfile"]["name"][$i]);
echo "第 ".($i+1)." 個檔案===>上傳檔案成功!<br/><br/>";
echo "<script>alert('第 '+($i+1)+' 個檔案===>上傳檔案成功!')</script>";
}
}
}
echo "<a href='javascript:history.go(-1)'>返回首頁</a>";
?>
<form method="post" action="bmi.php" enctype="Multipart/Form-data" name="form1">
<fieldset name="Group1">
<legend>BMI 的計算</legend>
<label id="Label1">姓名:</label> <input name="name1" type="text" />
<label id="Label2"><br />
體重:</label> <input name="num1" type="number" />
<label id="Label3"><br />
身高:</label> <input name="num2" type="number" />
<br />
<input name="Reset1" type="reset" value="reset" />
<input name="Submit1" type="submit" value="submit" />
</fieldset>
</form>
--------------------------------------
<?php // BMI 的計算結果 (bmi.php)
$name1 = $_POST['name1'];
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
// (4捨5入函式:round($bmi, 1))
$h = $num2/100;
$bmi = round($num1/($h * $h), 1);
echo $name1; ?><br />
<label id="Label1">BMI = </label><?php echo $bmi; ?>