開発環境
- OS X Lion - Apple(OS)
- Apache (Web Server)
- PHP (サーバーサイドプログラミング言語)
- TextWrangler(Text Editor) (BBEditの無料機能制限版、light版)
『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の8章(クッキーとセッションでユーザを記憶)8.8(演習問題)4を解いてみる。
4.
HTML、PHPのコード(TextWrangler)
<?php
session_start();
require 'formhelpers.php';
$products = array("C#" => 1,
"JavaScript" => 2,
"Python" => 3,
"Perl" => 4,
"Ruby" => 5,
"PHP" => 6);
if(! array_key_exists('_submit_check', $_POST)){
$_POST['_submit_check'] = 0;
}
if($_POST['_submit_check']){
if($form_errors = validate_form()){
show_form($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>";
}
if(!array_key_exists('order', $_SESSION)){
$_SESSION['order'] = 0;
}
if($_SESSION['order']){
$defaults = array();
foreach($products as $product => $n){
if(!array_key_exists($product, $_SESSION)){
$_SESSION[$product] = "";
}
$defaults[$product] = $_SESSION[$product];
}
} else {
$defaults = $_POST;
}
foreach($products as $product => $n){
print "商品番号 $n";
if(!array_key_exists($product, $defaults)){
$defaults[$product] = "";
}
input_text($product, $defaults);
print "<br />";
}
input_submit('submit', "submit");
print "<input type='hidden' name='_submit_check' value='1'/>";
print "</form>";
}
function validate_form(){
global $products;
$errors = array();
foreach($products as $product => $n){
if(strlen($_POST[$product]) &&
(($_POST[$product] != strval(intval($_POST[$product]))) ||
(intval($_POST[$product]) < 0))){
$errors[] = "エラー: 入力を確認してください。";
}
}
return $errors;
}
function process_form(){
global $products;
$_SESSION["order"] = 1;
foreach($products as $product => $n){
if(strlen($_POST[$product])){
$_SESSION[$product] = $_POST[$product];
}
}
print "送信ありがとうございました。<br />";
print '<a href="sample61.php">確認ページへ進む</a>';
}
?>
<?php
session_start();
require 'formhelpers.php';
$products = array("C#" => 1,
"JavaScript" => 2,
"Python" => 3,
"Perl" => 4,
"Ruby" => 5,
"PHP" => 6);
if(! array_key_exists('_submit_check', $_POST)){
$_POST['_submit_check'] = 0;
}
if($_POST['_submit_check']){
process_form();
} else {
show_form();
}
function show_form($errors = ''){
global $products;
if(!array_key_exists('order', $_SESSION)){
$_SESSION['order'] = 0;
}
if($_SESSION['order']){
print "送信内容<ul>";
$i = 1;
foreach($products as $product => $n){
if(array_key_exists($product, $_SESSION)){
print "<li>商品番号{$n}: " . $_SESSION[$product] . "</li>";
$i = 0;
}
}
print "</ul>";
if($i){
print "なし";
}
} else {
print "送信された商品はありません。";
}
print "<br />";
print "<a href='sample60.php'>注文ページに戻る</a><br />";
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
input_submit('submit', 'order');
print '<input type="hidden" name="_submit_check" value="1" />';
print '</form>';
}
function process_form(){
global $products;
unset($_SESSION['order']);
foreach($products as $product => $n){
unset($_SESSION[$product]);
}
print "確定しました。<br />";
print '<a href="sample60.php">注文ページに戻る</a>';
}
?>
HTMLソース
<form method='POST' action='/~kamimura/kamimura_blog/learning_php/sample60.php'>商品番号 1<input type="text" name="C#" value="" /><br />商品番号 2<input type="text" name="JavaScript" value="" /><br />商品番号 3<input type="text" name="Python" value="" /><br />商品番号 4<input type="text" name="Perl" value="" /><br />商品番号 5<input type="text" name="Ruby" value="" /><br />商品番号 6<input type="text" name="PHP" value="" /><br /><input type="submit" name="submit" value="submit"/><input type='hidden' name='_submit_check' value='1'/></form>
送信された商品はありません。<br /><a href='sample60.php'>注文ページに戻る</a><br /><form method="POST" action="/kamimura_blog/learning_php/sample61.php"><input type="submit" name="submit" value="確å®�"/><input type="hidden" name="_submit_check" value="1" /></form>
0 コメント:
コメントを投稿