開発環境
- OS X Lion - Apple(OS)
- Apache (Web Server)
- PHP (サーバーサイドプログラミング言語、スクリプト言語)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の8章(クッキーとセッションでユーザーを記憶)8.8(演習問題)4を解いてみる。
4.
HTML、PHPのソースコード(BBEdit)
<?php
session_start();
require 'formhelpers.php';
$products = array('javascript'=> "Learning Javascript",
'csharp' => 'Learning C#',
'python' => 'Learning Python',
'perl' => 'Learning Perl',
'ruby' => 'Learning Ruby',
'php' => 'Learning PHP');
if(array_key_exists('_submit_check', $_POST)){
if($errors = validate_form()){
show_form($errors);
} else {
process_form();
}
} else {
show_form();
}
function show_form($errors=''){
global $products;
print '<form method="POST" action="'. $_SERVER['PHP_SELF'] . '">';
if($errors){
print 'エラー<ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
$defaults = array();
if(array_key_exists('order', $_SESSION)){
foreach($products as $key => $value){
if(array_key_exists($key, $_SESSION)){
$defaults[$key] = $_SESSION[$key];
}
}
} else {
$defaults = $_POST;
}
foreach($products as $key => $value){
print '<label>' . $value . '<br />数量: ';
input_text($key, $defaults);
print '</label><br />';
}
input_submit('submit', 'order');
print '<input type="hidden" name="_submit_check" value="1" />';
print '</form>';
}
function validate_form(){
global $products;
$errors = array();
foreach($products as $key => $value){
if(strlen($_POST[$key])){
if($_POST[$key] != strval(intval($_POST[$key]))){
$errors[] = "数値を入力してください";
} elseif(intval($_POST[$key]) < 0) {
$errors[] = "0以上の数を入力してください";
}
}
}
return $errors;
}
function process_form(){
global $products;
$_SESSION['order'] = 1;
foreach($products as $key => $value){
if(array_key_exists($key, $_POST)){
$_SESSION[$key] = $_POST[$key];
}
}
print '<p>注文を受け付けました</p>';
print '<p><a href="./sample103.php">注文確認ページへ</a></p>';
}
?>
<?php
session_start();
require 'formhelpers.php';
$products = array('javascript'=> "Learning Javascript",
'csharp' => 'Learning C#',
'python' => 'Learning Python',
'perl' => 'Learning Perl',
'ruby' => 'Learning Ruby',
'php' => 'Learning PHP');
if(array_key_exists('_submit_check', $_POST)){
process_form();
} else {
show_form();
}
function show_form(){
global $products;
if(array_key_exists('order', $_SESSION)){
print '注文一覧<ul>';
foreach($products as $key => $value){
if(array_key_exists($key, $_SESSION)){
print '<li>' . $value . ' 個数: ' . $_SESSION[$key] . '</li>';
}
}
print '</ul>';
} else {
print '<p>注文はありません</p>';
}
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
input_submit("submit", "order");
print '<input type="hidden" name="_submit_check" value="1" />';
print '</form>';
print '<p><a href="./sample102.php">注文をやりなおす</a></p>';
}
function process_form(){
global $products;
unset($_SESSION['order']);
foreach($products as $key => $value){
unset($_SESSION[$key]);
}
print "<p>注文完了!</p>";
print '<p><a href="./sample102.php">注文を続ける</a></p>';
}
?>
0 コメント:
コメントを投稿