correction-ulysse #2
@@ -1,16 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
include 'include/controller.php';
|
include_once 'include/controller.php';
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="fr">
|
<html lang="fr">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<title></title>
|
<title>Exercice 3</title>
|
||||||
<link
|
<link
|
||||||
rel="stylesheet"
|
rel="stylesheet"
|
||||||
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
|
href="https://cdn.jsdelivr.net/npm/@picocss/pico.min.css"
|
||||||
/>
|
/>
|
||||||
<link rel="stylesheet" href="./css/style.css">
|
<link rel="stylesheet" href="./css/style.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -18,17 +18,42 @@ include 'include/controller.php';
|
|||||||
<h3>Calculatrice</h3>
|
<h3>Calculatrice</h3>
|
||||||
<form method="POST">
|
<form method="POST">
|
||||||
<div class="grid">
|
<div class="grid">
|
||||||
<input placeholder="un nombre" type="number" step="any" name="op1" value ="" required>
|
<input
|
||||||
|
placeholder="un nombre"
|
||||||
|
type="number"
|
||||||
|
step="any"
|
||||||
|
name="op1"
|
||||||
|
value="<?php echo htmlspecialchars($valeurOp1, ENT_QUOTES, 'UTF-8'); ?>"
|
||||||
|
required
|
||||||
|
>
|
||||||
<select name="operation" required>
|
<select name="operation" required>
|
||||||
<option value="+">+</option>
|
<option value="+" <?php echo $operation === '+' ? 'selected' : ''; ?>>+</option>
|
||||||
<option value="-">-</option>
|
<option value="-" <?php echo $operation === '-' ? 'selected' : ''; ?>>-</option>
|
||||||
<option value="x">x</option>
|
<option value="x" <?php echo $operation === 'x' ? 'selected' : ''; ?>>x</option>
|
||||||
<option value="/">/</option>
|
<option value="/" <?php echo $operation === '/' ? 'selected' : ''; ?>>/</option>
|
||||||
</select>
|
</select>
|
||||||
<input placeholder="un nombre" type="number" step="any" name="op2" required>
|
<input
|
||||||
<button type="submit" name="soumis"> Calculer</button>
|
placeholder="un nombre"
|
||||||
</grid>
|
type="number"
|
||||||
|
step="any"
|
||||||
|
name="op2"
|
||||||
|
value="<?php echo htmlspecialchars($valeurOp2, ENT_QUOTES, 'UTF-8'); ?>"
|
||||||
|
required
|
||||||
|
>
|
||||||
|
<button type="submit" name="soumis">Calculer</button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<?php if ($messageErreur !== '') : ?>
|
||||||
|
<p><?php echo htmlspecialchars($messageErreur, ENT_QUOTES, 'UTF-8'); ?></p>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if ($aCalcule) : ?>
|
||||||
|
<p>
|
||||||
|
Résultat :
|
||||||
|
<?php echo htmlspecialchars($valeurOp1, ENT_QUOTES, 'UTF-8'); ?>
|
||||||
|
</p>
|
||||||
|
<?php endif; ?>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,2 +1,56 @@
|
|||||||
<?php
|
<?php
|
||||||
//TODO
|
|
||||||
|
$op1Saisi = filter_input(INPUT_POST, 'op1', FILTER_UNSAFE_RAW);
|
||||||
|
$op2Saisi = filter_input(INPUT_POST, 'op2', FILTER_UNSAFE_RAW);
|
||||||
|
$operationSaisie = filter_input(INPUT_POST, 'operation', FILTER_UNSAFE_RAW);
|
||||||
|
|
||||||
|
$op1Saisi = is_string($op1Saisi) ? trim($op1Saisi) : '';
|
||||||
|
$op2Saisi = is_string($op2Saisi) ? trim($op2Saisi) : '';
|
||||||
|
$operationSaisie = is_string($operationSaisie) ? trim($operationSaisie) : '+';
|
||||||
|
|
||||||
|
$operationsAutorisees = ['+', '-', 'x', '/'];
|
||||||
|
$operation = in_array($operationSaisie, $operationsAutorisees, true) ? $operationSaisie : '+';
|
||||||
|
|
||||||
|
$resultat = null;
|
||||||
|
$messageErreur = '';
|
||||||
|
$aCalcule = false;
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||||
|
$op1 = filter_var($op1Saisi, FILTER_VALIDATE_FLOAT);
|
||||||
|
$op2 = filter_var($op2Saisi, FILTER_VALIDATE_FLOAT);
|
||||||
|
|
||||||
|
if ($op1 === false || $op2 === false) {
|
||||||
|
$messageErreur = 'Veuillez saisir deux nombres valides.';
|
||||||
|
} elseif (!in_array($operation, $operationsAutorisees, true)) {
|
||||||
|
$messageErreur = 'Veuillez choisir une opération valide.';
|
||||||
|
} elseif ($operation === '/' && (float) $op2 == 0.0) {
|
||||||
|
$messageErreur = 'La division par zéro est impossible.';
|
||||||
|
} else {
|
||||||
|
$aCalcule = true;
|
||||||
|
|
||||||
|
switch ($operation) {
|
||||||
|
case '+':
|
||||||
|
$resultat = $op1 + $op2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '-':
|
||||||
|
$resultat = $op1 - $op2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'x':
|
||||||
|
$resultat = $op1 * $op2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '/':
|
||||||
|
$resultat = $op1 / $op2;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$messageErreur = 'Veuillez choisir une opération valide.';
|
||||||
|
$aCalcule = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$valeurOp1 = $aCalcule ? (string) $resultat : $op1Saisi;
|
||||||
|
$valeurOp2 = $op2Saisi;
|
||||||
|
|||||||
Reference in New Issue
Block a user