Rechner

Eiggen

Grünschnabel
Hallo liebe Community.
Ich erstelle gerade einen rechner per PHP.
Leider nimmt das Formular nur die Addition an, das heißt egal welchen Button ich auswähle, es wird nur addiert.
was muss ich ändern? Vielen Dank im voraus

<!DOCTYPE hmtl>
<html lang="de">
<head>
<meta charset="utf-8"/>
<title>Rechner</title>
<?php
function addiere($summand1, $summand2) {
return ($summand1 + $summand2);
}
function subtrahiere($minuend, $subtrahend) {
return ($minuend - $subtrahend);
}
function multipliziere($faktor1, $faktor2) {
return ($faktor1 * $faktor2);
}
function dividiere($dividend, $divisor) {
if ($divisor !=0) {
return ($dividend / $divisor);
}
}
?>
</head>
<body>
<?php
if (isset($_POST["zahl1"])) {
$zahl1 = $_POST["zahl1"];
$zahl2 = $_POST["zahl2"];
$ergebnis = addiere($zahl1, $zahl2);
$ergebnis2 = subtrahiere($zahl1, $zahl2);
$ergebnis3 = multipliziere($zahl1, $zahl2);
$ergebnis4 = dividiere($zahl1, $zahl2);
echo "<h3>Das Ergebnis lautet $ergebnis</h3>";
}
?>
<form method = "post">
<p>Bitte geben Sie die beiden Zahlen in die Felder ein.</p>
<p><label for="zahl1">Zahl 1 </label>
<input type="text"
name = "zahl1"
required
placeholder=" Zahl 1"
autofocus/>
</p>
<p><label for="zahl2">Zahl 2 </label>
<input type="text"
name = "zahl2"
required
placeholder=" Zahl 2"
</p>
<p><input type = "radio" id="addiere" name="addiere" value="+" />
<label for ="addiere">+</label><br />
<input type = "radio" i#d="subtrahiere" name="subtrahiere" value="-" />
<label for ="subtrahiere">-</label><br />
<input type = "radio" id="multipliziere" name="multipliziere" value="*" />
<label for ="multipliziere">*</label><br />
<input type = "radio" id="dividiere" name="dividiere" value="/" />
<label for ="dividiere">/</label><br />
<input type = "submit" value="Rechnen" />
</p>
</form>
</body>
</html>
 
Mein Weg ist bestimmt auch nicht Optimal aber so geht es
PHP:
<?php
if (isset($_POST["zahl1"])) {
$zahl1 = $_POST["zahl1"];
$zahl2 = $_POST["zahl2"];
if (isset($_POST["addiere"])) {
$ergebnis = $zahl1+$zahl2;
}
if (isset($_POST["subtrahiere"])) {
$ergebnis = $zahl1-$zahl2;
}
if (isset($_POST["multipliziere"])) {
$ergebnis = $zahl1*$zahl2;
}
if (isset($_POST["dividiere"])) {
$ergebnis = $zahl1/$zahl2;
}
echo "<h3>Das Ergebnis lautet $ergebnis</h3>";
}
?>
 
Zurück