HSG

Aktuelle Seite: HSG/Fächer/Informatik/PHP

combo.php :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html>
<head>
<title>Formulardaten ein- und ausgeben</title>
<meta name="author" content="mk" />
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1" />
<?php
  if (isset($_POST["a"])) {$a = $_POST["a"];} else {$a = "";};
  if (isset($_POST["b"])) {$b = $_POST["b"];} else {$b = "";};
  if (doubleval($a) && doubleval($b))
  {
    $s = $a + $b;
  }
  else
  {
    $s = "";
  };
?>
</head>
<body >
<h4>Combo-Formular</h4>
<form action="combo.php" method="post">
<pre>
<label>a   </label><input type="text" name="a" value="<?php echo $a; ?>" /><br />
<label>b   </label><input type="text" name="b" value="<?php echo $b; ?>" /><br />
    <input type="submit" value="addiere" /><br />
<label>a+b </label><input type="text" name="s" value="<?php echo $s; ?>" /><br />
</pre>
</form>
<p><a href="http://validator.w3.org/check?uri=referer">valid?</a></p>
</body>
</html>

Die Funktion isset($var) gibt genau dann den Wahrheitswert wahr zurück, wenn die Variable $var existiert. Ihre Verwendung in obigem Script verhindert, dass beim ersten Aufruf eine Fehlermeldung ausgegeben wird, wenn $_POST["a"] nicht existiert.
Die Funktion doubleval($a) wandelt $a in eine reelle Zahl (double) um. Sie gibt genau dann wahr zurück, wenn die Umwandlung erfolgreich war.

Valid XHTML 1.0!