<?php
// Session starten
session_start();

// Variablen initialisieren
$NameFehler = $EmailFehler = $NachrichtFehler = $CaptchaFehler = FALSE;
$Fehlerfrei = TRUE;

// Formular wurde abgeschickt
if (isset( $_POST['senden'] ))
{
	// Felder auf Inhalt pruefen
	if (strlen( trim( $_POST['name'] ) ) < 3)
	{
		$Fehlerfrei = FALSE;
		$NameFehler = TRUE;
	}
	if (strlen( trim( $_POST['email'] ) ) < 3)
	{
		$Fehlerfrei = FALSE;
		$EmailFehler = TRUE;
	}
	if (strlen( trim( $_POST['nachricht'] ) ) < 3)
	{
		$Fehlerfrei = FALSE;
		$NachrichtFehler = TRUE;
	}
	if (md5( $_POST['sicherheitscode'] ) != $_SESSION['captcha_code'])
	{
		$Fehlerfrei = FALSE;
		$CaptchaFehler = TRUE;
	}
	
	// Wenn alle Felder ausgefuellt wurden und der CAPTCHA-Code korrekt war
	if ($Fehlerfrei)
	{
		// Code zum Email-Versand ausfuehren
		echo "<h2>Das Formular wurde korrekt ausgefuellt!</h2>";
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Kontaktformular mit CAPTCHA</title>

<style type="text/css">
<!--
body {font-size: 62.5%;}
fieldset {width: 235px; padding: 5px; border: solid 1px #CCCCCC;}
legend {font: bold 1.6em "Courier New", Courier, monospace; color: #0099FF;}
input, textarea {width: 220px;}
input.button {width: 150px; margin: 15px 0px 10px 40px; font: bold 1.2em Verdana, Arial, Helvetica, sans-serif;}
label {display: block; font: bold 1.1em Verdana, Arial, Helvetica, sans-serif; margin-top: 10px;}
img {margin: 15px 0 0 75px;}
-->
</style>
</head>

<body>
<form id="kontaktformular" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
	<fieldset>
    	<legend>Kontaktformular</legend>
        <label for="name"><?php echo $NameFehler ? '<span style="color: #FF0000;">Ihr Name:</span>' : 'Ihr Name:'; ?></label>
        	<input name="name" id="name" type="text" value="<?php echo $_POST['name']; ?>" />
        <label for="email"><?php echo $EmailFehler ? '<span style="color: #FF0000;">Ihre Email:</span>' : 'Ihre Email:'; ?></label>
        	<input name="email" id="email" type="text" value="<?php echo $_POST['email']; ?>" />
        <label for="nachricht"><?php echo $NachrichtFehler ? '<span style="color: #FF0000;">Ihre Nachricht:</span>' : 'Ihre Nachricht:'; ?></label>
        	<textarea rows="6" name="nachricht" id="nachricht"><?php echo $_POST['nachricht']; ?></textarea>
        <img src="captcha.php" alt="Sicherheitscode" title="Sicherheitscode" width="80" height="25" />
        <label for="sicherheitscode"><?php echo $CaptchaFehler ? '<span style="color: #FF0000;">Bitte Sicherheitscode eingeben:</span>' : 'Bitte Sicherheitscode eingeben:'; ?></label>
        	<input name="sicherheitscode" id="sicherheitscode" type="text" />
        <input name="senden" id="senden" type="submit" value="Absenden" class="button" />
	</fieldset>
</form>
</body>
</html>