tdphp/td4/td4b/exo4.php

94 lines
3.9 KiB
PHP
Raw Permalink Normal View History

2023-03-22 08:13:04 +01:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
</head>
<body>
<div class="container p-5">
<h1> Formulaire </h1>
<form class="pt-3" method="POST" action="#">
<div class="mb-3">
<label for="prenom" class="form-label">Prenom :</label>
<input type="text" class="form-control" id="prenom" name="prenom">
</div>
<div class="mb-3">
<label for="nom" class="form-label">Nom :</label>
<input type="text" class="form-control" id="nom" name="nom">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email :</label>
<input type="text" class="form-control" id="email" name="email">
</div>
<div class="mb-3">
<label for="url" class="form-label">URL :</label>
<input type="url" class="form-control" id="url" name="url">
</div>
<input name="userIP" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>" type="hidden">
<input name="userDATE" value="<?php echo date("Y-m-d H:i:s"); ?>" type="hidden">
<div class="mb-3">
<label class="form-label">Type de sortie :</label><br>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" id="choix1" name="sortie" value="txt">
<label class="form-check-label" for="choix1">Fichier texte</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" id="choix2" name="sortie" value="csv">
<label class="form-check-label" for="choix2">CSV</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" id="choix3" name="sortie" value="json">
<label class="form-check-label" for="choix3">JSON</label>
</div>
</div>
<button type="submit" name="valide" class="btn btn-primary">Envoyer</button>
</form>
<?php
if (isset($_POST['valide'])) {
echo "<h1> Réponse </h1>";
foreach ($_POST as $cle => $valeur) {
echo "<strong>$cle</strong> : $valeur <br>";
}
if ($_POST["sortie"] == "txt") {
file_put_contents("data2.txt", $_POST['prenom'] . " " . $_POST['nom'] . " " . $_POST['email'] . " " . $_POST['url'] . " " . $_POST['userIP'] . " " . $_POST['userDATE'] . "\n", FILE_APPEND);
} else if ($_POST["sortie"] == "csv") {
$fp = fopen('data2.csv', 'a');
fputcsv($fp, $_POST);
} else if ($_POST["sortie"] == "json") {
$actuel = file_get_contents('data2.json');
$listeTemp = json_decode($actuel);
if (is_array($listeTemp)) {
array_push($listeTemp, $_POST);
} else {
$listeTemp = array($_POST);
}
$donneesJson = json_encode($listeTemp);
file_put_contents('data2.json', $donneesJson);
}
}
?>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</body>
</html>