//------------------- HTML:

<div id="error" class="<?php 
if( $error !== '' ){
	echo 'showError';
} ?>"><?php echo $fehlerausgabe; ?></div>
<form name="skillflow" method="post" 
action="form_4.php" enctype="multipart/form-data">
	<span class="label">Anrede: </span>
	<select name="anrede" class="mySelect">
		<option value="frau" <?php 
		if( $error !== '' && $_POST['anrede'] == 'frau' ){ 
			echo 'selected'; 
		} 
		?>>Frau</option>
		<option value="herr" <?php 
		if( $error !== '' && $_POST['anrede'] == 'herr' ){ 
			echo 'selected'; 
		} 
		?>>Herr</option>
		<option value="divers" <?php 
		if( $error !== '' && $_POST['anrede']  == 'divers' ){ 
			echo 'selected'; 
		} 
		?>>Divers</option>
	</select>
	<br>
	<span class="label<?php 
	if($error == 'vorname'){ 
		echo ' label-error';
	} 
	?>">Vorname: </span>
	<input type="text" class="" name="vorname" value="<?php 
	if( isset( $_POST['send']) ){ 
		echo $_POST['vorname']; } 
	?>">
	<br>
	<span class="label<?php 
	if($error == 'name'){ 
		echo ' label-error';
	} 
	?>">Name: </span>
	<input type="text" class="" name="name" value="<?php 
	if( isset( $_POST['send']) ){ 
		echo $_POST['name']; 
	} 
	?>">
	<br>
	<span class="label<?php 
	if($error == 'email'){ 
		echo ' label-error';
	} ?>">E-Mail: </span>
	<input type="email" class="" name="email" value="<?php 
	if( isset( $_POST['send']) ){ 
		echo $_POST['email']; 
	} 
	?>">
	<br>
	<div class="nachricht-text<?php 
	if($error == 'kommentar'){ 
		echo ' label-error';
	} 
	?>">Nachricht:</div>
	<textarea name="kommentar" class="comment">
	<?php 
	if( isset( $_POST['send']) ){ 
		echo $_POST['kommentar']; 
	} 
	?></textarea>
	<br>
	<div class="vocal-text<?php if($error == 'vocal'){ 
		echo ' label-error';
	} ?>">
		Bitte entferne aus dem Feld unten alle Vokale.
	</div>
	<input type="text" name="vocal" class="vocal-input" value="
	<?php echo $fragetext; ?>">
	<br>
	<input type="submit" class="mybutton" name="send" value="Send">
	  
	<input type="reset" class="mybutton" value="Delete">
</form>



//------------------- CSS:

#error{
	width:100%;
	color:red;
	font-size:14px;
	font-weight:900;
	text-align:center;
	border:1px solid white;
	padding:5px 0;
	margin-bottom:15px;
	visibility:hidden;
}

.label{
	display:inline-block;
	width:75px;
	font-size:14px;
	padding:5px 3px;
	margin-right:5px;
	border:1px solid white;
}

select.mySelect{
	width:auto;
	color:black;
	padding: 1px 0;
	background-color: white;
	border:1px solid white;
}
option{
	color:black;
	background-color: white;
	padding:5px 0;
}

input[type="text"], 
input[type="email"]{
	width:200px;
	font-size:14px;
	padding:5px 0;
	color:black;
	background-color: white;
	border:1px solid white;
}

.nachricht-text{
	display:block;
	font-size:14px;
	margin:15px 0px 5px 0px;
}

textarea.comment{
	width: 95%;
    height: 150px;
	color:black;
	background-color: white;
	border:1px solid white;
}

.vocal-text{
	margin:10px 0px 0px 0px;
	
}

input.vocal-input{
	width: 99%;
    font-size: 18px;
    padding: 3px;
}

.mybutton {
    width: auto;
    padding: 2px 2px;
    color: black;
    background-color: white;
    cursor: pointer;
	border:1px solid white;
}


#error.showError{
	visibility:visible;
}

.label-error{
	font-weight:600;
	color:red !important;
}



//------------------- PHP:

<?php 
session_start();

// SESSION nur auf 0 setzen, wenn noch nicht vorhanden.
if ( !isset( $_SESSION[ 'count' ]) ) {
    $_SESSION[ 'count' ] = 0;
}

// Erstellen eines Arrays mit den Fragen
$frage    = [];
$frage[0] = 'Eine Eule ruht';
$frage[1] = 'Aurelia organisiert';
$frage[2] = 'In einer Oase';
$frage[3] = 'Leo und Julia';

// Erstellen eines Arrays mit den Antworten
$antwort    = [];
$antwort[0] = 'n l rht';
$antwort[1] = 'rl rgnsrt';
$antwort[2] = 'n nr s';
$antwort[3] = 'L nd Jl';

// Erste Frage zuweisen an die Variable $fragetext
$fragetext = $frage[ $_SESSION[ 'count' ] ];

// Zusätzliche Fehlerausgabe
$fehlerausgabe = 'Sie haben nicht alle Felder ausgefüllt';

$error = '';

if (isset($_POST['send'])) {

	$fields = [];
	$fields[0] = 'anrede'; 
	$fields[1] = 'vorname'; 
	$fields[2] = 'name'; 
	$fields[3] = 'email'; 
	$fields[4] = 'kommentar'; 

    foreach ( $fields as $field ) {
        $value = trim($_POST[$field]);

        if ($value === '') {
            $error = $field;
            break;
        }
    }

	if ( $error === '' ) {
		if( trim($_POST[ 'vocal' ]) == $antwort[ $_SESSION['count'] ] ) {
			// Weiter zum Mailversand
			
			$domaene 	= "DeineDomaion.de";
			$eol 		= PHP_EOL; // PHP-Zeilenende
			$absender	= "info@DeineDomaion.de";
			$to			= 'info@DeineDomaion.de';
			$From		= $absender;
			$bool		= false;
			$subject	= 'Nachricht vom Server';
			
			// Der Kontent
			$content = "--------------------------------------------------------".$eol;
			$content .= "Nachricht vom Server: ".$domaene.$eol;
			$content .= "--------------------------------------------------------".$eol.$eol;
			$content .= "Persoenliche Daten:".$eol;
			$content .= "Von: ".$_POST['anrede'].' '.$_POST['vorname']." ".$_POST['name'].$eol;
			$content .= "E-Mail: ".$_POST['email'].$eol;
			$content .= "--------------------------------------------------------".$eol;
			$content .= "--------------------------------------------------------".$eol;
			$content .= "Nachricht:".$eol.$eol;
			$content .= $_POST['kommentar'].$eol;
			$content .= "--------------------------------------------------------".$eol;
			
			// Der Header
			$Header = [];
			$Header[] = "MIME-Version: 1.0";
			$Header[] = "Content-type: text/plain; charset=iso-8859-1; format=flowed";
			$Header[] = "Content-Transfer-Encoding: quoted-printable";
			$Header[] = "From: {$From}";
			$Header[] = "Reply-To: {$absender}";
			$Header[] = "X-Mailer: PHP/".phpversion();
			
			// Versand
			$bool = mail($to,$subject,$content,implode($eol,$Header));						
			if( $bool ){
				// Rufe Danke-Seite "danke.php" auf
				header('Location: http://'.$_SERVER['HTTP_HOST'].'/danke.php');
				session_destroy();
				exit();
			} else {
				// Falls ein Fehler aufgtreten sein sollte
				$_SESSION[ 'count' ] = 0;
				$fragetext = $frage[ $_SESSION[ 'count' ] ];
				$fehlerausgabe = 'Die Mail wurde nicht versendet.';
			}
			
		} else {
			// START::: Prüfung - Bist Du Mensch
			$_SESSION['count']++;
			if( $_SESSION[ 'count' ] > 3 ) {
				$_SESSION[ 'count' ] = 0;
			}
			
			$fragetext = $frage[ $_SESSION[ 'count' ] ];
			$fehlerausgabe = 'Du bist kein Mensch';
			$error = 'vocal';
		}		
		// ENDE::: Prüfung - Bist Du Mensch
		
	}
}

?>
