-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
163 lines (132 loc) · 4.59 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
// This file is your starting point (= since it's the index)
// It will contain most of the logic, to prevent making a messy mix in the html
// This line makes PHP behave in a more strict way
declare(strict_types=1);
// debugging code
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
// We are going to use session variables so we need to enable sessions
session_start();
// Routing with _GET variable
$order = $_GET['order'] ?? 'gear';
// Use this function when you need to need an overview of these variables
function whatIsHappening()
{
echo '<h2>$_GET</h2>';
var_dump($_GET);
echo '<h2>$_POST</h2>';
var_dump($_POST);
echo '<h2>$_COOKIE</h2>';
var_dump($_COOKIE);
echo '<h2>$_SESSION</h2>';
var_dump($_SESSION);
}
// whatIsHappening();
// provide some products using an Associative Array
$gear = [
['name' => 'Camera: Sony HDRCX405 Camcorder ', 'price' => 229.99],
['name' => 'Microphone: Audio-Technica AT2020USB Plus ', 'price' => 29.99],
['name' => 'Tripod: iKan E-Image EG01A2 ', 'price' => 99.99],
['name' => 'Lighting: Flashpoint 19 Kit ', 'price' => 49.99],
['name' => 'Video editing software: Adobe Premiere Elements 2021', 'price' => 39.99],
];
$play = [
['name' => 'Football: Blue', 'price' => 29.99],
['name' => 'Basketball: Orange ', 'price' => 39.99],
['name' => 'Volleyball: White ', 'price' => 59.99],
['name' => 'Baseball: Grey ', 'price' => 49.99],
];
// ADD another product array, eg cameras only, or microphones only
// print_r($products);
$totalValue = 0;
function deliveryAddress()
{
if (isset($_POST['street'])) {
return $_POST['street'] . " " . $_POST['streetnumber'] . " , " . $_POST['city'] . " " . $_POST['zipcode'];
}
}
// This function will return a list/array of invalid fields back in form
function validate()
{
/**
* 1. Does it exist? or has it been submitted?
* 2. Is it empty? or does 'value === NULL'?
* 3. Display back to user => (Handle form)
*/
$emptyField = [];
// check for required empty fields
// This function will send a list of invalid fields back
if (empty($_POST['email'])) {
$emptyField[] = 'email';
}
if (empty($_POST['street'])) {
$emptyField[] = 'street';
}
if (empty($_POST['streetnumber'])) {
$emptyField[] = 'streetnumber';
}
if (empty($_POST['city'])) {
$emptyField[] = 'city';
}
if (empty($_POST['zipcode'])) {
$emptyField[] = 'zipcode';
}
if (empty($_POST['products'])) {
$emptyField[] = 'products';
}
$_SESSION['email'] = $_POST['email'];
$_SESSION['street'] = $_POST['street'];
$_SESSION['streetnumber'] = $_POST['streetnumber'];
$_SESSION['city'] = $_POST['city'];
$_SESSION['zipcode'] = $_POST['zipcode'];
$_SESSION['products'] = $_POST['products'];
// TODO: include empty strings to all values when empty
return $emptyField;
}
function handleForm($products, &$totalValue)
{
// Validation (step 2)
$invalidFields = validate();
if (!empty($invalidFields)) {
$message = '';
foreach ($invalidFields as $invalidField) {
$message .= "Please provide your {$invalidField}.";
$message .= '<br>';
}
// TODO: show the previous values in the form so that the user doesn't have to retype everything
return [
'errors' => true,
'message' => $message
];
} else {
// Order Confirmation
$productNumbers = array_keys($_POST['products']);
$productNames = [];
foreach ($productNumbers as $productNumber) {
$productNames[] = $products[$productNumber]['name'];
$totalValue += $products[$productNumber]['price'];
}
$message = 'Your order is : ' . implode(', ', $productNames);
$message .= '<br>';
$message .= '<br>';
$message .= 'Your address : ' . $_POST['street'] . ' ' . $_POST['streetnumber'] . ', ' . $_POST['zipcode'] . ' ' . $_POST['city'];
return [
'errors' => false,
'message' => $message,
'totalValue' => $totalValue
];
}
}
// Check order sent
// handle successful submission
// return "order will be set if the form has been submitted (to TRUE)";
// isset() method in PHP to test the form is submitted successfully or not
$formSubmitted = isset($_POST['order']);
$result = [];
if ($formSubmitted) {
$result = handleForm(${$order}, $totalValue);
// this will either return address & products or a list of arrays
}
require 'form-view.php';