PHP Beispiele und Übungen: Code
<?php
$titel = "PHP Formularverarbeitung";
$untertitel = "Array der Formulareingaben mit foreach abarbeiten";
$pagename = basename($_SERVER["PHP_SELF"]);
$strbtnlblsubmit = "Abschicken";
?>
<?php include 'incdoctype.php'?>
<html>
<head>
<?php include 'incheader.php'?>
<title><?php echo "$titel: $untertitel"?></title>
</head>
<body>
<div class="main">
<h1><?php echo "$titel: $untertitel"?></h1>
<?php
if ($_POST != null) {
echo "<h2>Ihre Eingaben</h2>\n<table border=\"1\">\n";
foreach($_POST as $strfield => $values) {
echo "<tr>\n";
echo "<td valign=\"top\">\n";
echo "$strfield<br />\n";
echo "</td>\n";
echo "<td valign=\"top\">\n";
if (is_array($_POST[$strfield])) {
$blnfirst = true;
foreach ($values as $value){
if (!$blnfirst) {
echo "<br />\n";
} else {
$blnfirst = false;
}
echo $value . " ";
}
} else {
echo $values . " ";
}
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<h2>Formular
</h2>
<form method="post" action="<?php echo $pagename ?>">
<p>Text
<input type="text" name="frmfldtext" size="20"
value="<?php echo $_POST["frmfldtext"]?>" />
</p>
<p>Dropdown mit Mehrfachauswahl
</p>
<p>
<select size="3" name="frmflddropdown[]" multiple="">
<option value="1" <?php if ($_POST["frmflddropdown"] != null && in_array(1, $_POST["frmflddropdown"])) {
echo "selected=\"\"";} ?>>
Auswahl 1
</option>
<option value="2" <?php if ($_POST["frmflddropdown"] != null && in_array(2, $_POST["frmflddropdown"])) {
echo "selected=\"\"";} ?>>
Auswahl 2
</option>
<option value="3" <?php if ($_POST["frmflddropdown"] != null && in_array(3, $_POST["frmflddropdown"])) {
echo "selected=\"\"";} ?>>
Auswahl 3
</option>
</select>
</p>
<p>Dropdown ohne Mehrfachauswahl
</p>
<p>
<select size="3" name="frmflddropdownohne">
<option value="1" <?php if ($_POST["frmflddropdownohne"] == "1") {
echo "selected=\"\"";} ?>>
Auswahl 1
</option>
<option value="2" <?php if ($_POST["frmflddropdownohne"] == "2") {
echo "selected=\"\"";} ?>>
Auswahl 2
</option>
<option value="3" <?php if ($_POST["frmflddropdownohne"] == "3") {
echo "selected=\"\"";} ?>>
Auswahl 3
</option>
</select>
</p>
<p>
<input type="radio" name="frmfldoption" value="A" <?php
if ($_POST["frmfldoption"] == "A") {
echo "checked=\"\"";} ?> />A
<input type="radio" name="frmfldoption" value="B" <?php
if ($_POST["frmfldoption"] == "B") {
echo "checked=\"\"";} ?> />B
</p>
<p>
<input type="checkbox" name="frmfldcheckbox" value="angekreuzt" <?php
if ($_POST["frmfldcheckbox"] == "angekreuzt") {
echo "checked=\"\"";} ?> />Kontrollkästchen
</p>
<p><input type="submit" name="btnsubmit"
value="<?php echo $strbtnlblsubmit?>" />
</p>
</form>
<?php include 'incfooter.php'?>
</div>
</body>
</html>
Demo
Zurück zur Liste mit PHP-Beispielen
Zurück zu www.ecotronics.ch
Impressum und Datenschutzerklärung