PHP和HTML Form表單處理

PHP 處理 HTML 表單的方式是表單的任何元素都在 PHP 腳本中自動生效。以下是 HTML 表單的範例:

HTML Form

ex2.htm (copy到 c:\wamp\www目錄下)

<html lang="zh-Hant-TW">
<head>
 <meta charset="utf-8">
<title>練習 2-1</title>
</head>
<body>
<form method="POST" action="ex2-1.php">
<p><input type="text" name="T1" size="20"></p>
<p><input type="text" name="T2" size="20"></p>
<p><input type="submit" value="送出" name="B1">
<input type="reset" value="重新設定" name="B2"></p>
</form>
</body>
</html>

PHP

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<title>Php Ex2-1</title>
</head>
<body>
<?
$Var1=htmlspecialchars($_POST["T1"]);
$Var2=(int)$_POST['T2'];
echo "您的輸入是:$Var1";
?>
</body>
</html>

htmlspecialchars() 使得 HTML 之中的特殊字符被正確的編碼,從而不會被使用者在頁面注入 HTML 標籤或者 Javascript 代碼。例如T2欄位,我們明確知道他是一個數值,因此我們將它轉換為一個整形值(integer)來自動的消除任何不必要的字符。Global變數 $_POST,它包含了所有的 POST 數據。請注意我們的表單提交數據的方法(method)。如果使用了 GET 方法,那麼表單中的信息將被儲存到超全局變量 $_GET 中。如果並不關心請求數據的來源,也可以用超全局變量 $_REQUEST,它包含了所有 GET、POST、COOKIE 和 FILE 的數據。

PHP表單處理

vote1.php(ver.1)

<html lang="zh-Hant-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>畢業旅行投票</title>
</head>
<body>

<p>畢業旅行投票</p>
<form method='post' action='confirm1.php'>


<table border='1' width='100%' id='table1'>
    <tr>
        <td align='right' width='200'>學號</td>
        <td><input type='text' name='SID' size='10'> </td>
    </tr>
    <tr>
        <td align='right' width='200'>姓名</td>
        <td><input type='text' name='SName' size='10'> </td>
    </tr>
    <tr>
        <td align='right' width='200'>身份證末四碼</td>
        <td><input type='text' name='SCode' size='10'></td>
    </tr>
    <tr>
        <td align='right' width='200'>選擇地點</td>
        <td><input type='radio' value='澎湖' name='SLoc'>澎湖
            <input type='radio' value='花蓮' name='SLoc'>花蓮
            <input type='radio' value='泰國' name='SLoc'>泰國</td>
    </tr>
    <tr>
        <td align='right' width='200'>意見</td>
        <td><input type='text' name='SComment' size='50'> </td>
    </tr>
    <tr>
        <td align='right' width='200'> </td>
        <td><input type='submit' name='Submit' value='投票'> </td>
    </tr>
</table>
</form>
</body>

</html>

confirm1.php(ver.1)

<?php
//將表單元件的值轉成php變數
  $Var1=htmlspecialchars($_POST["SID"]);
  $Var2=htmlspecialchars($_POST["SName"]);
  $Var3=htmlspecialchars($_POST["SCode"]);
  $Var4=htmlspecialchars($_POST["SLoc"]);
  $Var5=htmlspecialchars($_POST["SComment"]);
?>
<html lang="zh-Hant-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>畢業旅行投票確認</title>
</head>

<body>

<p>畢業旅行投票-確認投票</p>
<form method='post' action='save1.php'>

<?php
print "
<table border='1' width='100%' id='table1'>
    <tr>
        <td align='right' width=200>學號</td>
        <td><input type='hidden' name='SID' value='$Var1'>$Var1</td>
    </tr>
    <tr>
        <td align='right' width=200>姓名</td>
        <td><input type='hidden' name='SName' value='$Var2'>$Var2</td>
    </tr>
    <tr>
        <td align='right' width=200>身份證末四碼</td>
        <td><input type='hidden' name='SCode' value='$Var3'>$Var3</td>
    </tr>
    <tr>
        <td align='right' width=200>選擇地點</td>
        <td><input type='hidden'  name='SLoc' value='$Var4'>$Var4</td>
    </tr>
    <tr>
        <td align='right' width=200>意見</td>
        <td><input type='hidden' name='SComment' value='$Var5'>$Var5</td>
    </tr>
    <tr>
        <td align='right' width=200> </td>
        <td>
        ";
if ($Var4=='')
   echo "沒有選擇地點<a href='javascript:history.back()'>,請回上一頁重新填寫</a>";
else
   echo "若要更改<a href='javascript:history.back()'>,請回上一頁重新填寫</a>";
echo "
        </td>
    </tr>
</table>";
if (empty($Var1)||empty($Var2)||empty($Var3)||empty($Var4))//要有前四欄資料才能存檔
print "
<table border='1' width='100%' id='table1'>
    <tr>
        <td align='center'>
            <input type='hidden' name='SMethod' value='insert' >
            <input type='submit' name='Submit'  value='確認投票' disabled='disabled'> 
        </td>
    </tr>
</table>
";
else
print "
<table border='1' width='100%' id='table1'>
    <tr>
        <td align='right'>
            <input type='hidden' name='SMethod' value='insert' >
            <input type='submit' name='Submit' value='確認投票'> 
        </td>
    </tr>
</table>
";
?>

</form>
</body>

</html>

save1.php(ver.1)

<?php
    //將表單元件的值轉成php變數
    $Var1=htmlspecialchars($_POST["SID"]);
    $Var2=htmlspecialchars($_POST["SName"]);
    $Var3=htmlspecialchars($_POST["SCode"]);
    $Var4=htmlspecialchars($_POST["SLoc"]);
    $Var5=htmlspecialchars($_POST["SComment"]);
    $Var6=$_POST["SMethod"];
?>

<html lang="zh-Hant-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>畢業旅行投票</title>
</head>


<?php
//將表單元件的值存入資料庫
//
//
?>

<body>

<p>畢業旅行投票-投票完成</p>

<?php
echo "
<table border='1' width='100%' id='table1'>
    <tr>
        <td align=right width=200>學號</td>
        <td>$Var1</td>
    </tr>
    <tr>
        <td align=right width=200>姓名</td>
        <td>$Var2</td>
    </tr>
    <tr>
        <td align=right width=200>身份證末四碼</td>
        <td>$Var3</td>
    </tr>
    <tr>
        <td align=right width=200>選擇地點</td>
        <td>$Var4</td>
    </tr>
    <tr>
        <td align=right width=200>意見</td>
        <td>$Var5</td>
    </tr>
</table>";
?>
</form>
<hr>
<a href='list1.php'>查看報名資料</a>
</body>

</html>

list1.php(ver.1)

<html lang="zh-Hant-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>畢業旅行投票資料</title>
</head>

<body>

<p>畢業旅行投票資料</p>
<table border="1" width="100%" id="table1">
    <tr>
        <td>學號</td>
        <td>姓名</td>
        <td>選擇地點</td>
        <td>投票時間</td>
        <td>意見</td>
    </tr>
    <tr>
        <td>000000000</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>000000000</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
</table>
<hr>
投票結果
<table border="1" width="100%" id="table2">
    <tr>
        <td>澎湖</td>
        <td>000</td>
    </tr>
    <tr>
        <td>花蓮</td>
        <td>000</td>
    </tr>
    <tr>
        <td>泰國</td>
        <td>000</td>
    </tr>
</tr>
</table>
</body>

</html>

PHP語法說明

PHP 資料結構-關連陣列(PHP Associative Arrays)

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
$age = array();
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
foreach($age as $x => $x_value) {
    echo "Key=" . $x . ", Value=" . $x_value;
    echo "<br>";
}

PHP 流程控制-if

<?php
if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";
}
?>

PHP 流程控制-for

for ($i = 1; ; $i++) {
    if ($i > 10) {
        break;
    }
    echo $i;
}

PHP 流程控制-foreach

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?>

PHP 字串函數

<?php
$mystring = 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo "The string '$findme' was not found in the string '$mystring'";
} else {
    echo "The string '$findme' was found in the string '$mystring'";
    echo " and exists at position $pos";
}
?>

PHP 時間函數

//今天
$dateStr = date("Y-m-d");
//本週一~週日的日期
$dtBegin = new DateTime();
$dtBegin->modify('monday this week');
$begin1 = $dtBegin->format('Y-m-d');
$dtEnd = new DateTime();
$dtEnd->modify('sunday this week');
$end1 = $dtEnd->format('Y-m-d');
//下週
$dtBegin->modify('+7 day');
$begin2 = $dtBegin->format('Y-m-d');
$dtEnd->modify('+7 day');
$end2 = $dtEnd->format('Y-m-d');

results matching ""

    No results matching ""