isset($_POST[...]) tylko sprawdza czy pole w formularzu o podanej nazwie istniało (input, select, textarea itp.) i zostało zwrócone jako zmienna do $_POST w momencie wysłania formularza, nie sprawdza co "zawiera" zmienna, natomiast empty(...) uznaje wartość jako pustą, gdy zmienna zawiera (przyjęła wartość):
- '', "",
- null,
- array(),
- FALSE,
- NULL,
- '0', "0", 0
proponuję w ogóle zrezygnować z nich (isset, empty) w tym przypadku, na etapie pobierania wartości ze zmiennych z $_POST i użyć operatora ?? "do obsługi" ewentualnych błędów.
przykład (pozwoliłem sobie nieco przerobić Twój skrypt - mam nadzieje, że nie będziesz miał Mi tego za złe )
<?php
// var_dump($_POST); // Dla "podejrzenia" danych przysłanych z formularza
$data_show = $result_show = '';
$numA = $numB = 0;
function checkInputs($a, $b)
{
$error_message = '';
if (trim($a) == '' || trim($b) == '')
$error_message = 'some of the fields are empty, fill in the fields';
else if (!is_numeric($a) || !is_numeric($b))
$error_message = 'enter only numbers, please';
else if ($a == 0 && $b == 0)
$error_message = 'both of the filds contain zero value';
if ($error_message)
return $error_message;
else
return null;
}
// wykonaj kod tylko, gdy skrypt został uruchomiony przez
// wysłanie formularza po naciśnięciu przycisku submit ( checkPHP )
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$numA = $_POST["numA"] ?? null;
$numB = $_POST["numB"] ?? null;
if (! $result_show = checkInputs($numA, $numB))
{
$add = ((float)$numA + (float)$numB);
$subtract = ((float)$numA - (float)$numB);
$divided = ($numB == 0) ? '<span class="error">division by zero error</span>': ((float)$numA / (float)$numB);
$multiply = ((float)$numA * (float)$numB);
$data_show = "For <b>{$numA}</b> and <b>{$numB}</b>";
$result_show = 'Addition: ' . (($add % 1 == 0) ? $add : number_format($add, 2)) . '<br>'
. 'Subtraction: ' . (($subtract % 1 == 0) ? $subtract : number_format($subtract, 2)) . '<br>'
. 'Division: ' . ((!is_numeric($divided)) ? $divided : (($divided % 1 == 0) ? $divided : number_format($divided, 2))) . '<br>'
. 'Multiplication: ' . (($multiply % 1 == 0) ? $multiply : number_format($multiply, 2));
}
}
?>
<!DOCTYPE html>
<html lang="pl-PL">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<link rel="stylesheet" href="styl.css">
<title>TEST</title>
<style>
input[type="number"] {
border: 1px solid red;
margin-right: 0.5em;
width: 5em;
}
span.error {
font: 700 0.9em/1 monospace;
color: orangered;
}
</style>
</head>
<body>
<form action="" method="post">
<label for="numA">type a:</label>
<input type="number" id="numA" name="numA" min="0" max="99999" step="0.01" value="" required>
<label for="numB">type b:</label>
<input type="number" id="numB" name="numB" min="0" max="99999" step="0.01" value="" required>
<button type="button" id="divide">/</button>
<button type="button" id="subtraction">-</button>
<input type="submit" value="checkPHP">
<input type="reset" value="RESET">
</form>
<div id="data"><?php echo $data_show; ?></div>
<div id="result"><?php echo $result_show; ?></div>
<script>
let timeoutId;
const divide_button = document.querySelector('#divide');
divide_button.addEventListener('click', divide);
const subtraction_button = document.querySelector('#subtraction');
subtraction_button.addEventListener('click', subtraction);
const reset_button = document.querySelector('input[type="reset"]');
reset_button.addEventListener('click', allReset);
const data_show = document.querySelector('#data'),
result_show = document.querySelector('#result');
function divide()
{
const numA = document.querySelector('#numA').value,
numB = document.querySelector('#numB').value;
if (checkInputs(numA, numB))
{
if (numB == 0)
{
showMessage('division by zero error');
return false; // natychmiastowe opuszczenie funkcji
}
const result = numA / numB;
showResult(result);
}
}
function subtraction()
{
const numA = document.querySelector('#numA').value,
numB = document.querySelector('#numB').value;
if (checkInputs(numA, numB))
{
const result = numA - numB;
showResult(result);
}
}
function showResult(result)
{
if (Number.isInteger(result)) // result % 1 === 0
result_show.innerHTML = result;
else
result_show.innerHTML = result.toFixed(2);
}
function checkInputs(a, b)
{
let error_message = '';
if (a === null || a === '' || b === null || b === '' )
error_message = 'some of the fields are empty, fill in the fields';
else if (Number.isNaN(a) || Number.isNaN(b))
error_message = 'enter only numbers, please';
else if (a == 0 && b == 0)
error_message = 'both of the filds contain zero value';
if (error_message)
{
showMessage(error_message);
return false;
} else {
return true;
}
}
function showMessage(message)
{
allReset();
result_show.innerHTML = message;
clearTimeout(timeoutId);
timeoutId = setTimeout(allReset, 3000); // po 3 sekundach "wyczyść" komunikat
}
function allReset()
{
data_show.innerHTML = '';
result_show.innerHTML = '';
}
</script>
</body>
</html>
BTW,
Show a number to two decimal places
Use round() if you are expecting a number in float format only, else use number_format()
[ How do I check that a number is float or integer? ]
[ PHP Ternary Operator - $result = condition ? value1 : value2; ]
[ What does double question mark (??) operator mean in PHP ]