ursus contionabundo: ich habe das fehler gefunde

Beitrag lesen

ich möchte diese Woche lerne die bedeutung von jeder Zeile von deinem Code,

Der Code ist bei Programmen das einfachste. Die Schwierigkeit besteht darin, ein Programm zu planen und dann

  1. beim Schreiben im Kopf zu behalten, was welcher Teil macht;
  2. was wo gespeichert ist;
  3. beim Plan zu bleiben.

Datei "ClassProveContakt2" - an dieser habe ich folgendes verändert:

  1. Die header und Einstellungen gehören NICHT in eine Klassendatei (hier: ClassProveContakt2.php), sondern in den Hauptteil.
  2. <form>-tag hinzugefügt.
  3. Das Datum wird als erster Eintrag in der Tabelle angezeigt.
<?php
class ClassProveContakt2 {

      private $Name     = '';
      private $Email    = '';
      private $Message  = '';
      private $PostOk   = false;
      private $DateTime = false;
      private $items    = false;

      function __construct() {
      
          $this -> DateTime = date('m/d/Y h:i:s a');
          $this -> items = ['Name', 'Email', 'Message'];  
          
          $flag = true;
          foreach ( $this -> items as $key ) {
			        if ( empty ( $_POST[$key] ) )  {
			            $flag = false;
			        } else {
			           $this -> $key = trim( filter_var( $_POST[$key], FILTER_SANITIZE_STRING ) );
			       }
		      }	    
          $this -> PostOk = $flag;
      }
  
      function ShowForm() {

        echo '<form method="POST">'
        . '<label for="Name">Name: </label><input type="text" name="Name" id="Name" value="' . htmlspecialchars( $this -> Name ) . '"><br>'
        . '<label for="Email">Email: </label><input type="email" name="Email" id="Email" value="' . htmlspecialchars( $this -> Email ) . '"><br>'
        . '<label> Message: </label><br><textarea cols="45" rows="6" name="Message">'. htmlspecialchars( $this -> Message ) . '</textarea>'
        . '<br><br>'
        . '<input type="submit" name="post" value="POST COMMENT" id="comment">'
        . '</form>;
      }

      function ShowData () {
          if( $this -> PostOk ) {
          
             $ShowItems = $this -> items; 
             array_unshift( $ShowItems, 'DateTime' );
             
             echo '<table><tr>';
             foreach ( $ShowItems as $ColName ) {
                 echo"<th>" . htmlspecialchars( $ColName ) . "</th>";
             }
             echo '</tr><tr>';
             
             foreach ( $ShowItems as $ColName ) {
    			      echo"<td>" . htmlspecialchars( $this -> $ColName ) . "</td>";
    	       }
             echo "</tr></table>";
         } else {
             echo '<h3>*** Please enter all required fields ***</h3>';
         }
      }
}
# Nicht ohne wichtigen Grund mit ?> beenden. Das führt oft zu Problemen!

Datei kontakt.php - An dieser habe ich folgendes verändert:

  1. Header und Einstellungen eingefügt.
  2. require_once() statt include(). Grund: Benötigte Klassen müssen genau einmal geladen werden.
<?php

header('Content-Type: text/html; Charset=utf-8');
mb_internal_encoding('UTF-8');
date_default_timezone_set('Europe/Berlin');
error_reporting(E_ALL);

require_once 'ClassProveContakt2.php';
$objekt = new ClassProveContakt2;

?><!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>kommentar</title>
<style>
table, th, td {
   border: 1px solid black;
   border-collapse: collapse;
}

th {
    font-weight: bold;
    text-align: left;
}
</style>
</head>
<body>

<h1>Formular:</h1>
<?php 
  $objekt -> ShowForm(); 
?>
<h1>Daten:</h1>
<?php 
   $objekt -> ShowData(); 
?>

</body>
</html>