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
| <?php
$data_file = __DIR__ . '/data.csv';
if (isset($_POST['last_name'], $_POST['first_name'])) { // Le formulaire a-t-il été soumis ?
$stream = fopen($data_file, 'a'); // Si oui on ouvre le fichier de données
fputcsv($stream, [$_POST['last_name'], $_POST['first_name']], ',', '"', ''); // On y ajoute les valeurs soumises en CSV
fclose($stream);
}
$stream = fopen($data_file, 'r'); // Ouverture du fichier de données
?>
<!doctype html>
<meta charset="utf-8">
<title>Mon premier formulaire</title>
<form method="post">
<input type="text" name="last_name" placeholder="Nom">
<input type="text" name="first_name" placeholder="Prénom">
<input type="submit">
</form>
<table border="1">
<tr>
<th>Nom</th>
<th>Prénom</th>
</tr>
<?php while ($record = fgetcsv($stream, 0, ',', '"', '')): // Parcour du fichier de données ?>
<tr>
<td><?= htmlspecialchars($record[0]) ?></td>
<td><?= htmlspecialchars($record[1]) ?></td>
</tr>
<?php endwhile ?>
</table> |
Partager