Bonjour à tous,

Pour une application de généalogie, je crée un formulaire sur plusieurs pages, initialement basé sur ce modèle. Ce modèle ne comporte pas de gestion d'erreurs. Pour la gestion des erreurs, je me suis donc tourné en complément sur celui-ci. J'ai également du apporter des modifications pour mes propres besoins.
Le but du formulaire est de permettre à un utilisateur de proposer des mises à jour (créations ou modifications.
La première page permet d'identifier le demandeur, les pages suivantes permettent de saisir les données à créer ou modifier.

Mon problème est que la variable de session contenant les données d'une page s'efface lorsque je reviens en arrière sur une étape précedente. Les autres variables de session persistent, ce n'est donc pas un problème de session_start() dans la page parentPage.php.

Voici le code de la page mère updateQuery.php (elle même incluse dans une page mère plus générale parentPage.php).
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
 
// Demande de mise à jour d'une personne
 
// A FAIRE
/*
	Faire test des erreurs
	Ajouter les contrôles en JavaScript
*/
 
##################################################################### SECTION TETE : PARAMETRAGE AUTORISATION ##########################################################
 
if (!isset($isIncluded)) {
	header('Location: '.URL_SITE.INDEX_PAGE);
	exit;
}
 
##################################################################### Fin SECTION TETE : PARAMETRAGE AUTORISATION ######################################################
 
##################################################################### SECTION MODELE ###################################################################################
 
connect();
 
// Liste des colonnes d'une table
function getColumnsFromTable($table) {
	global $db;
 
	$query = "SELECT COLUMN_NAME AS col
		FROM information_schema.COLUMNS
		WHERE TABLE_NAME='$table'
	;";
	$result = $db->query($query);
	return $result->fetchAll(PDO::FETCH_ASSOC);
}
 
function getPersonTitle($id) {
	global $db;
 
	$dateFormat = LOCAL_DATE_FORMAT;
	$query = "
		SELECT
			CONCAT(COALESCE(first_name, ''), ' ', COALESCE(last_name, '')) AS full_name,
			CONCAT('(clef=', id, ')') AS full_id
		FROM dat_persons
		WHERE id=:id
		LIMIT 1
	;";
	$result = $db->prepare($query);
	$result->bindParam(':id', $id);
	$result->execute();
	return $result->fetch();
}
 
function getPerson($id) {
	global $db;
 
	$dateFormat = LOCAL_DATE_FORMAT;
	$query = "
		SELECT id, gender, last_name, first_name, middle_name, DATE_FORMAT(birth_date, '$dateFormat') AS birth_date, birth_place,
			DATE_FORMAT(christening_date, '$dateFormat') AS christening_date, DATE_FORMAT(death_date, '$dateFormat') AS death_date, death_place,
			profession, comment, father_id, mother_id, birth_order
		FROM dat_persons
		WHERE id=:id
		LIMIT 1
	;";
	$result = $db->prepare($query);
	$result->bindParam('id', $id);
	$result->execute();
	return $result->fetch(PDO::FETCH_ASSOC);
}
 
##################################################################### FIN section MODELE ###############################################################################
 
##################################################################### SECTION CONTROLE #################################################################################
 
define('CFG_FORM_ACTION', URL_SITE.'parentPage.php?childPageKey=11');
define('REQ_FORMS', ['req_sender.php', 'req_person.php', 'req_biog.php', 'req_marriage.php', 'req_send']);
define('STAGE_NB', count(REQ_FORMS));
define('CFG_STAGE_ID', filter_input(INPUT_GET, 'stage', FILTER_VALIDATE_INT, [ 'options' => ['default'=>0, 'min_range'=>0, 'max_range'=>STAGE_NB] ]) );
define('CFG_BACK_FORM', "Location:".CFG_FORM_ACTION."&stage=".strval(CFG_STAGE_ID-1) );
define('CFG_NEXT_FORM', CFG_FORM_ACTION."&stage=".strval(CFG_STAGE_ID+1) );
define('NAV_LNK', "<a href='".CFG_FORM_ACTION."&stage=%d'>%d - %s</a>");
 
// Suppression des balises html
function cleanPost($field) {
	if ( is_string($field) ) {
		$field = strip_tags($field);
	}
	if ( is_array($field) ) {
		$field = array_map('strip_tags', $field);
	}
	return $field;
}
 
function filterPost() {
	foreach ($_POST as $postName=>&$postValue) {
		if ( strpos($postName, "src_") === 0 ) {
			$toSave[$postName] = (int) $postValue;
		}
		switch ($postName) {
			// Clés
			case 'sender_id':
			case 'idMain':
			case 'father_id':
			case 'mother_id':
			case 'birth_order':
				$toSave[$postName] = (int) $postValue;
				if ( !empty($postValue) and $toSave[$postName] === 0 ) {
					$errors[] = sprintf(REQ_ERR_KEYN, "<q>$postValue</q>");
				}
				break;
			case 'sender_mail':
				$toSave[$postName] = filter_input(INPUT_POST, $postName, FILTER_SANITIZE_EMAIL);
				break;
			// Téléphone de l'expéditeur
			case 'sender_phone':
				$toSave[$postName] = filter_input(INPUT_POST, $postName, FILTER_SANITIZE_NUMBER_INT);
				break;
			// Dates
			case 'birth_date':
			case 'death_date':
			case 'christening_date':
				$toSave[$postName] = convertDateFromEurToSQL($_POST[$postName], NULL);
				if ( $toSave[$postName] === false ) {
					$errors[] = sprintf(REQ_ERR_DATE, "<q>$postValue</q>");
				}
				break;
			// Données textuelles ou non définies
			default:
				$toSave[$postName] = filter_input(INPUT_POST, $postName, FILTER_SANITIZE_STRING);
				break;
		}
	}
	return $toSave;
}
 
// Création du menu du haut de la page
function makeMenuH() {
	$items = [];
	$_SESSION['updateQuery']['maxVisitedStage'] = empty($_SESSION['updateQuery']['maxVisitedStage']) ? 0: $_SESSION['updateQuery']['maxVisitedStage'];
	if ($_SESSION['updateQuery']['maxVisitedStage'] < CFG_STAGE_ID) {
		$_SESSION['updateQuery']['maxVisitedStage'] = CFG_STAGE_ID;
	}
	foreach(REQ_NAV_H as $formId => $formName) {
		if ($formId >= $_SESSION['updateQuery']['maxVisitedStage']) {
			$items[] = "<span>".strval($formId + 1)." - $formName</span>";
		}
		else {
			$items[] = sprintf(NAV_LNK, $formId+1, $formId+1, $formName);
		}
	}
	// Remplacement de la valeur définie dans la boucle précédente pour forcer le lien sur 'Résumé'
	$navigH = implode('</li><li>', $items);
	return "<ul class='menuH'><li>$navigH</li></ul>";//'<ul class="menuH"><li>'.$navigH.'</li></ul>';
}
 
// Fin des fonctions et des définitions ....................................................................................................................
 
$errors[0] = '';
$errors = ( !empty($_SESSION['updateQuery']['errors']) ) ? $_SESSION['updateQuery']['errors']: [''];
 
// Récupération et contrôle de l'id de la personne à éditer
if (isset($_POST['idMain'])) {
	$_SESSION['updateQuery']['idMain'] = (int) $_POST['idMain'];
	if ( ! checkIfIdIsInAbo($_SESSION['updateQuery']['idMain']) ) {
		$errors[1] = sprintf(insertFrSpace(REQ_ERR_KEYN), "<q>{$_SESSION['updateQuery']['idMain']}</q>");
		goto endPHP;
	}
}
elseif (isset($_POST['new'])) {
	$_SESSION['updateQuery']['idMain'] = 0;
}
if ( !isset($_SESSION['updateQuery']['idMain'] ) ) {
	$errors[] = REQ_ERR_NOBODY;
	goto endPHP;
}
$idMain = $_SESSION['updateQuery']['idMain'];
unset($_POST['idMain'], $_POST['edit'], $_POST['new']);
 
$navigH = makeMenuH();
 
// Nettoyage des données envoyées par le $_POST
if (!empty($_POST)) {
	$_POST = array_map('cleanPost', $_POST);
}
 
// Récupération des informations, affichage des sous-formulaires
 
switch(CFG_STAGE_ID) {
 
	// Ouverture étape 1 (Etat-civil), Contrôle et enregistrement du demandeur (formulaire 0)
	case 1:
		unset($_POST['next']);
 
		// Contrôle des saisies
		if ( ! checkIfIdIsInAbo( (int) $_POST['sender_id'] ) ) { $errors[] = sprintf(insertFrSpace(REQ_ERR_KEYN), "<q>{$_POST['sender_id']}</q>"); }
		if (empty($_POST['sender_lastname'])) { $errors[] = insertFrSpace(REQ_ERR_FROM_LAST); }
		if (empty($_POST['sender_firstname'])) { $errors[] = insertFrSpace(REQ_ERR_FROM_FIRST); }
		if (empty($_POST['sender_mail'])) {
			if ( empty($_POST['sender_address']) or empty($_POST['sender_country']) or empty($_POST['sender_postcode']) or empty($_POST['sender_locality']) ) {
				$errors[] = insertFrSpace(REQ_ERR_FROM_ADDRESSES);
			}
		}
		if ($_POST['sender_mail'] and !ctl_email($_POST['sender_mail'])) { $errors[] = insertFrSpace(REQ_ERR_MAIL); }
		if (!ctl_postcode($_POST['sender_postcode'])) { $errors[] = insertFrSpace(REQ_ERR_ZIPCODE); }
		$_SESSION['updateQuery'][CFG_STAGE_ID-1] = filterPost();
 
		// Retour au formulaire précédent en cas d'erreur
		if ( count($errors) >1 ) {$_SESSION['updateQuery']['errors'] = $errors; header(CFG_BACK_FORM); exit; }
		break;
 
	// Ouverture étape 2 (biographie), contrôle et enregistrement de l'état-civil (formulaire 1)
	case 2:
		unset($_POST['next']);
 
		// Contrôle des saisies
		if (empty($_POST['gender'])) { $errors[] = insertFrSpace(REQ_ERR_GENDER); }
		if (empty($_POST['last_name'])) { $errors[] = insertFrSpace(REQ_ERR_LAST); }
		if (empty($_POST['first_name'])) { $errors[] = insertFrSpace(REQ_ERR_FIRST); }
		if ($_POST['last_name'] == '?' and $_POST['first_name'] == '?') {
			$errors[] = insertFrSpace(REQ_ERR_NO_NAME);
		}
		$_SESSION['updateQuery'][CFG_STAGE_ID-1] = filterPost();
 
		// Retour au formulaire précédent en cas d'erreur
		if ( count($errors) >1 ) {$_SESSION['updateQuery']['errors'] = $errors; header(CFG_BACK_FORM); exit; }
		break;
 
	// Ouverture étape 3 (marriages), contrôle et enregistrement de la biographie (formulaire 2)
	case 3:
		// Contrôle des saisies
		if ($_POST['text'] and empty($_POST['author'])) { $errors[] = insertFrSpace(REQ_ERR_AUTHOR); }
		if ($_POST['text'] and empty($_POST['sources'])) { $errors[] = insertFrSpace(REQ_ERR_SOURCES); }
		$_SESSION['updateQuery'][CFG_STAGE_ID-1] = filterPost();
 
		// Retour au formulaire précédent en cas d'erreur
		if ( count($errors) >1 ) {$_SESSION['updateQuery']['errors'] = $errors; header(CFG_BACK_FORM); exit; }
		break;
 
	// Mariages
	case 4:
		break;
 
	// Envoi formulaire / Enregistrement
	case STAGE_NB:
		break;
 
	default:
		break;
}
 
$form = 'includes/forms/'.REQ_FORMS[CFG_STAGE_ID];
 
endPHP:
 
// Titre général du formulaire
$personTitle = getPersonTitle($idMain);
$h2 = ( !empty($personTitle) ) ? sprintf(REQ_H2_PARAM, $personTitle->full_name, $personTitle->full_id): REQ_H2_NEW;
 
// Définition des options de boutons radio src_*
$srcOptions = '';
foreach (REQ_SRC_OPTIONS as $value=>$text) {
	$srcOptions .= "<option value='$value'>$text</option>".PHP_EOL;
}
 
##################################################################### FIN section CONTROLE #############################################################################
 
##################################################################### SECTION VUE ######################################################################################
 
?>
 
<h2><?= $h2 ?></h2>
 
<?php
if ( isset($_SESSION['updateQuery']['errors']) ) {
	echo '<p>'.displayErrors($_SESSION['updateQuery']['errors']).'</p>';
	unset($_SESSION['updateQuery']['errors']);
}
?>
 
<?= $navigH; ?>
 
<h3><?= REQ_NAV_H[CFG_STAGE_ID]; ?></h3>
 
<p class="note"><?= MUST_FIELD ?></p>
 
<?php
if (file_exists($form))
	require_once($form);
?>
 
<script src="libraries/common.js"></script>
 
<?php
 
##################################################################### FIN section VUE ##################################################################################
Et un sous formulaire
Code php : Sélectionner tout - Visualiser dans une fenêtre à part
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
<?php
 
// Demande de mise à jour d'une personne - Form 1
 
##################################################################### SECTION TETE : PARAMETRAGE AUTORISATION ##########################################################
 
if (!isset($isIncluded)) {
	header('Location: '.URL_SITE.INDEX_PAGE);
	exit;
}
 
##################################################################### Fin SECTION TETE : PARAMETRAGE AUTORISATION ######################################################
 
##################################################################### SECTION MODELE ###################################################################################
 
connect();
 
// checks if a column exists in the table
function columnExists($column){
	global $db;
 
	$query = "SELECT COUNT(*) AS cpt
		FROM information_schema.COLUMNS
		WHERE COLUMN_NAME='$column' and TABLE_NAME='lst_countries'
	;";
	$result = $db->query($query);
	$exists = $result->fetch();
	if (boolval($exists->cpt)) return true;
	return false;
}
 
##################################################################### FIN section MODELE ###############################################################################
 
##################################################################### SECTION CONTROLE #################################################################################
 
// Initialisation des données du formulaire
if ( !empty($_SESSION['updateQuery'][CFG_STAGE_ID]) ) { $_POST = $_SESSION['updateQuery'][CFG_STAGE_ID]; }
else {
	$_POST = array_merge( $_POST, array_fill_keys([
	'sender_id',
	'sender_lastname',
	'sender_firstname',
	'sender_mail',
	'sender_address',
	'sender_country',
	'sender_postcode',
	'sender_locality',
	'sender_phone' ,
	'message',
	], '' ) );
}
 
// Récupération de la liste des pays en base de données
$colName = "en_name";
if (columnExists($language."_name")) {
	$colName = $language."_name";
}
$objCountries = new readTable($db, 'lst_countries', ['id', $colName], 1);
$objCountries->set_filter('filter', 1);
$countries = $objCountries->get_data();
 
// Création de la liste de choix des pays
$lstCountries = new optListSelect("sender_country");
$lstCountries->addOption(new optListOption(''));
foreach ($countries as $country) {
	$selected = '';
	if (isset($_POST['sender_country']) and strtoupper($_POST['sender_country']) == strtoupper($country['id'])) {
		$selected = ' selected';
	}
	$lstCountries->addOption(new optListOption($country[$colName], $country['id'], $selected));
}
unset($selected, $country);
 
##################################################################### FIN section CONTROLE #############################################################################
 
##################################################################### SECTION VUE ######################################################################################
 
?>
 
<form method="post" action="<?= CFG_NEXT_FORM; ?>" class="L">
 
	<label for="sender_id" class="mustField"><?= REQ_LBL_KEY ?>&nbsp;<sup>(a)</sup></label>
		<input type="text" name="sender_id" id="sender_id" value="<?= $_POST['sender_id']; ?>" placeholder="1234" required /><br/>
	<label for="sender_lastname" class="mustField"><?= REQ_LBL_LAST ?></label>
		<input type="text" name="sender_lastname" id="sender_lastname" value="<?= $_POST['sender_lastname']; ?>" required /><br/>
	<label for="sender_firstname" class="mustField"><?= REQ_LBL_FIRST ?></label>
		<input type="text" name="sender_firstname" id="sender_firstname" value="<?= $_POST['sender_firstname']; ?>" required /><br/>
	<label for="sender_mail"><?= REQ_LBL_MAIL ?>&nbsp;<sup>(b)</sup></label>
		<input type="text" name="sender_mail" id="sender_mail" value="<?= $_POST['sender_mail']; ?>" /><br/>
	<label for="sender_address"><?= REQ_LBL_ADDRESS ?>&nbsp;<sup>(b)</sup></label>
		<textarea name="sender_address" id="sender_address"><?= $_POST['sender_address']; ?></textarea><br/>
	<label for="sender_country"><?= REQ_LBL_COUNTRY ?>&nbsp;<sup>(b)</sup></label>
		<?= $lstCountries ?><br/>
	<label for="sender_postcode"><?= REQ_LBL_ZIP ?>&nbsp;<sup>(b)</sup></label>
		<input type="text" name="sender_postcode" id="sender_postcode" value="<?= $_POST['sender_postcode']; ?>" /><br/>
	<label for="sender_locality"><?= REQ_LBL_LOCALITY ?>&nbsp;<sup>(b)</sup></label>
		<input type="text" name="sender_locality" id="sender_locality" value="<?= $_POST['sender_locality']; ?>" /><br/>
	<label for="sender_phone"><?= REQ_LBL_PHONE ?>&nbsp;<sup>(c)</sup></label>
		<input type="text" name="sender_phone" id="sender_phone" value="<?= $_POST['sender_phone']; ?>" /><br/>
	<label for="message"><?= REQ_LBL_MESSAGE; ?></label>
		<textarea name="message" id="message" /><?= $_POST['message']; ?></textarea><br/>
	<p class="note">
		<sup>(a)</sup>&nbsp;<?= REQ_NOTE_FOR_KEY ?><br/>
		<sup>(b)</sup>&nbsp;<?= REQ_NOTE_FOR_ADDRESSES ?><br/>
		<sup>(c)</sup>&nbsp;<?= REQ_NOTE_FOR_PHONE ?><br/>
	</p>
	<p><input type="submit" name="next" value="<?= BTN_NEXT; ?>" /></p>
 
</form>
 
<?php
 
##################################################################### FIN section VUE ##################################################################################