iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 24
0
Modern Web

網頁程式設計so easy系列 第 24

Day23 [SESSION、檔案處理、電子郵件處理範例-3]

http://ithelp.ithome.com.tw/upload/images/20161222/20103385PAaOENnCdF.png


save.php(顯示表單頁之ajax執行之php)

http://ithelp.ithome.com.tw/upload/images/20161222/20103385WLZqEFA7R4.png

程式碼:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>save.php</title>

</head>
<body>
<?php
header("Content-Type:text/html; charset=utf-8");
session_start();
$file = "formData.csv";
$content = "姓名,電話,生日,Email,地址\r\n".$_SESSION["name"].",".$_SESSION["tel"].",".$_SESSION["birthday"].",".$_SESSION["email"].",".$_SESSION["address"];
$fp=fopen($file,"w")
	or exit ("檔案 $file 開啟錯誤<br/>");

//fwrite($fp, iconv('UTF-8', 'BIG5',$content));  //註1
//fwrite($fp, $content);  //註2

//如果使用註2Excel會出現亂碼,excel 出現亂碼的原因是檔首無 BOM(https://en.wikipedia.org/wiki/Byte_order_mark)
//普通Txt檔可使用註2
//註1是另一種解法
if(fwrite($fp, "\xEF\xBB\xBF".$content) ){
	print "寫入檔案 $file 成功<br/>";
}else{
	print "寫入檔案 $file 錯誤<br/>";
}
fclose($fp);

?>
</body>
</html>

結果:

  1. 頁面
    http://ithelp.ithome.com.tw/upload/images/20161224/201033856Wwhmwgzn8.png

  2. csv檔
    http://ithelp.ithome.com.tw/upload/images/20161224/201033854jp0FUWyIK.png

參考資料:http://chiahan.logdown.com/posts/280868/web-php-export-csv-chinese-encoding-problem
解決PHP印出csv有亂碼


上一篇
Day22 [SESSION、檔案處理、電子郵件處理範例-2]
下一篇
Day24 [SESSION、檔案處理、電子郵件處理範例-4]
系列文
網頁程式設計so easy30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言