<?php
/*
Template Name: Formular
*/
?>
<?php get_header(); ?>
 
 
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
 
 
<div class="post page">
  <h2><?php the_title(); ?></h2>
  <?php the_content(); ?>
</div>
 
 
<?php endwhile; ?>
<?php endif; ?>

<?php
session_start();

// Zufällige Zahlen für die Rechenaufgabe generieren
$zahl1 = rand(1, 10);
$zahl2 = rand(1, 10);
$_SESSION['captcha_result'] = $zahl1 + $zahl2;

$meldung = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $vorname = trim($_POST['vorname']);
    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $betreff = trim($_POST['betreff']);
    $nachricht = trim($_POST['nachricht']);
    $captcha = intval($_POST['captcha']);

    if (empty($vorname) || empty($name) || empty($email) || empty($betreff) || empty($nachricht)) {
        $meldung = "Bitte alle Felder ausfüllen!";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $meldung = "Ungültige E-Mail-Adresse!";
    } elseif ($captcha !== $_SESSION['captcha_result']) {
        $meldung = "Falsche Antwort auf die Rechenaufgabe!";
    } else {
        // Hier könnte der Mailversand erfolgen
        $to = "deine@email.de";
        $subject = "Kontaktanfrage: $betreff";
        $message = "Von: $vorname $name \nE-Mail: $email\n\n$nachricht";
        $headers = "From: $email";
        
        if (mail($to, $subject, $message, $headers)) {
            $meldung = "Nachricht erfolgreich gesendet!";
        } else {
            $meldung = "Fehler beim Senden der Nachricht.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kontaktformular</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 500px; margin: auto; }
        label { display: block; margin-top: 10px; }
        input, textarea { width: 100%; padding: 8px; margin-top: 5px; }
        button { background-color: #28a745; color: white; padding: 10px; border: none; margin-top: 10px; cursor: pointer; }
        button:hover { background-color: #218838; }
        .error { color: red; }
        .success { color: green; }
    </style>
</head>
<body>
    <h2>Kontaktformular</h2>
    <?php if (!empty($meldung)) { echo "<p class='" . (strpos($meldung, 'erfolgreich') !== false ? 'success' : 'error') . "'>$meldung</p>"; } ?>
    <form method="post">
        <label for="vorname">Vorname:</label>
        <input type="text" name="vorname" required>
        
        <label for="name">Name:</label>
        <input type="text" name="name" required>
        
        <label for="email">E-Mail:</label>
        <input type="email" name="email" required>
        
        <label for="betreff">Betreff:</label>
        <input type="text" name="betreff" required>
        
        <label for="nachricht">Nachricht:</label>
        <textarea name="nachricht" rows="5" required></textarea>
        
        <label>Rechenaufgabe: <?php echo $zahl1 . " + " . $zahl2; ?> = ?</label>
        <input type="number" name="captcha" required>
        
        <button type="submit">Senden</button>
    </form>
</body>
</html>