SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Exercises (XI)

PHP Exercise #21

Setting, using and unsetting a session

Write a program that shows a form that lists six products and shows, next to each product, a text field where the user can type the desired amount for each product. When the form is sent, the data has to be stored in the session. Create another page where the saved data is displayed, containing as well a link to the page where the order form is and another link to end the session. When clicking on the link that opens the order form, the form should be showing the data stored in the session and when clicking in the log out link, the data should be deleted from the session.

<?php
// Code placed at the beginning of both pages
session_start();
?>

<?php
require 'scripts/formhelpers.php';
$products = array('cuke' => 'Braised Sea Cucumber',
'stomach' => "Sauteed Pig's Stomach",
'tripe' => 'Sauteed Tripe with Wine Sauce',
'taro' => 'Stewed Pork with Taro',
'giblets' => 'Baked Giblets with Salt',
'abalone' => 'Abalone with Marrow and duck Feet');
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>';
}
// Creates an array of default values if there is an order stored in the session
if ($_SESSION['saved_order']) {
$defaults = array();
foreach ($products as $product => $description) {
$defaults["dish_$product"] = $_SESSION["dish_$product"];
}
} else {
$defaults = $_POST;
}
foreach ($products as $product => $description) {
input_text("dish_$product", $defaults);
print " $description<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 $product => $description) {
// If a value has been facilitated in the text field...
if (strlen($_POST["dish_$product"]) &&
// and it is not a valid integer...
(($_POST["dish_$product"] != strval(intval($_POST["dish_$product"]))) ||
// or it is less than zero...
intval($_POST["dish_$product"]) < 0)) {
// then it is not valid
$errors[] = "Please enter a valid quantity for $description.";
}
}
return $errors;
}
function process_form() {
global $products;
$_SESSION['saved_order'] = 1;
foreach ($products as $product => $description) {
if (strlen($_POST["dish_$product"])) {
$_SESSION["dish_$product"] = $_POST["dish_$product"];
}
}
print 'Thank you for your order. <br/><br/>';
print 'Check the link to see the order: <a href="test_order.php">Stored Order</a>';
}
?>
Braised Sea Cucumber
Sauteed Pig's Stomach
Sauteed Tripe with Wine Sauce
Stewed Pork with Taro
Baked Giblets with Salt
Abalone with Marrow and duck Feet