博客來網路書店查詢

書名

博客來網路書店查詢

星期一, 10月 18, 2010

PHP&MySQL--2 2010年10月課程講義預覽 修改

PHP&MySQL--2
一、 表單與資料接收的練習
1. text與password 表單【form1.html】:

〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form1" method="post" action="1.php" 〉
username:〈input type="text" name="username" size="6"〉〈br〉
password:〈input type="password" name="passwd" maxlength="6" size="10"〉〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉




接收:


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉〈/head〉〈body〉
〈?
echo "1:".$_POST['username']."〈br〉";
echo "2:".$_POST['passwd']."〈br〉";
echo "3:".$username."〈br〉";
echo "4:".$passwd."〈br〉";
echo "5:".$_GET['username']."〈br〉";
echo "6:".$_GET['passwd']."〈br〉";
?〉
〈/body〉〈/html〉



2. 選擇鈕
表單:


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form3" method="get" action="3.php" 〉
sex:
〈input type="radio" name="sex" value="boy"〉公的
〈input type="radio" name="sex" value="girl"〉母的
〈br〉
Blood:
〈input type="radio" name="blood" value="O"〉O
〈input type="radio" name="blood" value="A"〉A
〈input type="radio" name="blood" value="B"〉B
〈input type="radio" name="blood" value="AB" checked 〉AB
〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉 〈/form〉
〈/body〉
〈/html〉



接收:


〈html〉
〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
sex is 〈? echo $_GET['sex'];?〉〈br〉
blood is 〈? echo $_GET['blood'];?〉〈br〉
〈/body〉
〈/html〉




checked與disabled屬性的設定
3. 核選框
表單:


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form3" method="get" action="4.php" 〉
〈input type="checkbox" name="a" value="a"〉選項1
〈input type="checkbox" name="b" value="b"〉選項2
〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉 〈/form〉
〈/body〉
〈/html〉



接收:


〈html〉
〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
a is 〈? echo $_GET['a'];?〉〈br〉
b is 〈? echo $_GET['b'];?〉〈br〉
〈/body〉
〈/html〉



4. 下拉式選單
表單:


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form6" method="get" action="6.php" 〉
please select:
〈select name="travel" size="1"〉
〈option value="1"〉台北烏來〈/option〉
〈option〉台北101〈/option〉
〈option〉台北林家花園〈/option〉
〈option value="4" selected〉D〈/option〉
〈option value="5"〉E〈/option〉
〈option value="6"〉F〈/option〉
〈option value="7"〉G〈/option〉
〈option value="8"〉H〈/option〉
〈option value="9"〉I〈/option〉
〈/select〉〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈/form〉
〈/body〉
〈/html〉



selected與disabled屬性的設定
接收:


〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=UTF-8"〉
〈title〉request select〈/title〉〈/head〉
〈body〉
〈? echo $_GET['travel'];?〉
〈/body〉〈/html〉



二、 表單傳遞的問題:表單資料必須透過網址列才可傳送到第三頁
1. 表單網頁【請將form1.html改存成form2.html】


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form2" method="post" action="form1.php" 〉
username:〈input type="text" name="username" size="6"〉〈br〉
password:〈input type="password" name="passwd" maxlength="6" size="10"〉〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉



2. 接收資料,並在下方加入連結【請將1.php改存成form1.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉〈/head〉
〈body〉〈?
echo "1:".$_POST['username']."〈br〉";
echo "2:".$_POST['passwd']."〈br〉";
$myname=$_POST['username'];
if($_POST['username']=="") die("沒有帳號");
?〉〈a href="form2.php"〉123〈/a〉
〈a href="form2.php?username=〈? echo $myname; ?〉"〉123〈/a〉
〈/body〉
〈/html〉



3. form2.php如果不經form1.php的情況下可以顯示資料嗎?

〈html〉
〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉〈/title〉
〈/head〉
〈body〉
是否有登入帳號的資訊?
〈?
echo "post方式傳送的帳號:".$_POST['username']."〈br〉"; ?〉
透過網址列傳送帳號,具有危險性:
〈?
echo "網址列傳送的帳號:".$_GET['username']."〈br〉"; ?〉
〈/body〉
〈/html〉




三、 表單傳遞的問題:使用者可在本機儲存表單html送至Server


〈html〉
〈head〉
〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉
〈body〉
〈form name="form2" method="post" action="http://localhost/class1/form1.php" 〉
username:〈input type="text" name="username" value="haha" size="6"〉〈br〉
password:〈input type="password" name="passwd" maxlength="6" size="10" value="cc"〉〈br〉
〈input type="submit" value="ok"〉〈input type="reset" value="cancel"〉
〈input type="button" value="what?"〉
〈/form〉
〈/body〉
〈/html〉



四、 介紹checkbox分組之前,先介紹陣列
1. 陣列初始化

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列〈/title〉〈/head〉
〈body〉〈?
$chinese[0] = 80;
$chinese[1] = 60;
$chinese[2] = 90;
$chinese[3] = 50;
$chinese[4] = 70;
for ($a=0; $a〈5; $a++)
echo "$chinese[$a] 〈br〉" ;
?〉〈/body〉〈/html〉




2. 請問以下陣列,網路硬碟上所提供的資料是否有缺?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列,不給予索引值編號〈/title〉〈/head〉
〈body〉〈?
$chinese[ ] = 80;
$chinese[ ] = 60;
$chinese[ ] = 90;
$chinese[ ] = 50;
$chinese[ ] = 70;
for ($a=0;$a〈5;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉



3. 陣列可用array方式規劃,網路硬碟上所提供的資料是否有缺?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立陣列,索引值編號任意給〈/title〉〈/head〉
〈body〉〈?
$chinese=array(
1=〉80,
3=〉60,
6=〉90,
8=〉50,
9=〉70
);
for ($a=0;$a〈=10;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉



4. 陣列可用文字做為索引,不過要如何看到全部資料?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立文字型態索引值陣列〈/title〉〈/head〉
〈body〉〈?
$a=array(
"Jan" =〉 "一月",
"Feb" =〉 "二月",
"Mar" =〉 "三月"
);
echo $a["Mar"]."〈br〉";
?〉〈/body〉〈/html〉



五、 for與foreach差別
1. 由「陣列可用array方式規劃」練習修改後比較,有何差別?

for ($a=0;$a〈=10;$a++)
echo $chinese[$a]."〈br〉" ; foreach ($chinese as $value1)
echo $value1."〈br〉";




2. 由「陣列可用文字做為索引」練習修改,加入foreach讀取所有資料


foreach ($a as $value1)
echo $value1."〈br〉";



3. 由「陣列可用array方式規劃」練習修改,更快速顯示索引值與內容


foreach ($chinese as $key1 =〉$value1)
echo "座號".$key1."同學的成績為:".$value1."〈br〉" ;



六、 session start():檔名為session1.php

〈? session_start(); ?〉
〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 session start()〈/title〉〈/head〉〈body〉 〈?
echo '〉〉第一頁〈〈';
$_SESSION['color'] = '漂亮';
$_SESSION['city'] = '台北';
$_SESSION['time'] = date("Y年m月d日 H時i分s秒");
echo '〈br /〉〈a href="session2.php"〉第二頁〈/a〉';
?〉〈/body〉〈/html〉




檔名為session2.php


〈? ob_start();//output buffering on
session_start(); //啟動 session
?〉
〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 session start() page2〈/title〉〈/head〉〈body〉 〈?
echo '〉〉第二頁〈〈 〈br /〉';
echo $_SESSION['color']."〈br〉";
echo $_SESSION['city']."〈br〉";
echo date("Y年m月d日 H時i分s秒")." ==〉 ".$_SESSION['time'];
echo '〈br/〉〈a href="session1.php"〉第一頁〈/a〉';
?〉〈/body〉〈/html〉



七、 php5的時間調整:date.timezone -〉 Asia/Tokyo或Asia/Taipei
八、 session時間的初始化


〈? //ob_start();
session_start(); //啟動 session
ini_set('date.timezone','Asia/Tokyo');
?〉



備註:session暫存路徑修改 ini_set('session.save_path','../');
九、 其他

沒有留言: