Skip to content

Instantly share code, notes, and snippets.

@BlackScorp
Created February 5, 2020 19:06
Show Gist options
  • Save BlackScorp/baea5a9d3ebe4c436bac9c8ae2a1ba56 to your computer and use it in GitHub Desktop.
Save BlackScorp/baea5a9d3ebe4c436bac9c8ae2a1ba56 to your computer and use it in GitHub Desktop.
Code zum Video : https://youtu.be/VBBZtB0kuKI
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
/**
* Eingabe
*/
$options = [
'help'=> ['title'=> 'Benötige allgemeine Hilfe','selected'=>false],
'feedback'=> ['title'=> 'Benötige Feedback zum Script','selected'=>false],
'bug'=> ['title'=> 'Fehler entdeckt','selected'=>false],
'business'=> ['title'=> 'Geschäftsanfrage','selected'=>false],
'others'=> ['title'=> 'Sonstiges','selected'=>false],
];
$name = '';
$message = '';
$mrsIsSelected = false;
$mrIsSelected = false;
$isRobot = false;
$isPostRequest = $_SERVER['REQUEST_METHOD'] === 'POST';
$errors =[];
/**
* Verarbeitung
*/
if($isPostRequest){
$name = filter_input(INPUT_POST,'name',FILTER_SANITIZE_STRING);
$message = filter_input(INPUT_POST, 'message', FILTER_SANITIZE_STRING);
$subject = filter_input(INPUT_POST, 'subject', FILTER_SANITIZE_STRING);
$mrsIsSelected = filter_input(INPUT_POST, 'salutation') === 'mrs';
$mrIsSelected = filter_input(INPUT_POST, 'salutation') === 'mr';
$isRobot = filter_input(INPUT_POST, 'robot') === 'on';
if(isset($options[$subject])){
$options[$subject]['selected'] = true;
}
if(!$name){
$errors[] = 'Name ist leer';
}
if (!$mrIsSelected && !$mrsIsSelected) {
$errors[] = 'Anrede auswählen';
}
if (!$message) {
$errors[] = 'Bitte eine Nachricht hinterlassen';
}
if (!isset($options[$subject])) {
$errors[] = 'Bitte betreff auswählen';
}
if(!$isRobot && count($errors) === 0){
$salutation = 'Mrs';
if ($mrIsSelected) {
$salutation = 'Mr';
}
$data = [
'salutation' => $salutation,
'name' => $name,
'subject'=>$options[$subject]['title'],
'message'=>$message
];
file_put_contents('datei.txt', serialize($data) . ';;;', FILE_APPEND);
$_POST = [];
$mrIsSelected = false;
$mrsIsSelected = false;
$options[$subject]['selected'] = false;
$subject = null;
$isRobot = false;
$name = '';
$message = '';
}
}
/**
* Ausgabe
*/
?>
<!DOCTYPE html>
<html lang="de">
<head>
<title>Kontakt</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<header class="jumbotron">
<div class="container">
<h1 class="display-4">Kontakt</h1>
<p class="lead">Schreib mir eine Nachricht!</p>
</div>
</header>
<?php if($isPostRequest):?>
<section class="container" id="alets">
<?php if(count($errors) === 0):?>
<div class="alert alert-success" role="alert">
Anfrage versendet!
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<?php endif;?>
<?php if(count($errors) > 0):?>
<div class="alert alert-danger" role="alert">
Fehler beim versenden der Anfrage
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
<?php foreach($errors as $errorMessage):?>
<p><?= $errorMessage ?></p>
<?php endforeach;?>
</div>
<?php endif;?>
</section>
<?php endif;?>
<section class="container" id="contactForm">
<form method="post">
<div class="card">
<div class="card-header">
Kontaktformular
</div>
<div class="card-body">
<div class="row form-group">
<div class="col-2">
Anrede:
</div>
<div class="col">
<div class="form-check form-check-inline">
<input name="salutation" id="salutationMrs" class="form-check-input" type="radio" value="mrs"<?= $mrsIsSelected ? ' checked' : '' ?>><label class="form-check-label" for="salutationMrs"> Frau</label>
</div>
<div class="form-check form-check-inline">
<input name="salutation" id="salutationMr" class="form-check-input" type="radio" value="mr"<?= $mrIsSelected ? ' checked' : '' ?>><label class="form-check-label" for="salutationMr"> Herr</label>
</div>
</div>
</div>
<div class="row form-group">
<label class="col-2 col-form-label" for="name">
Name:
</label>
<div class="col">
<input type="text" value="<?= htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>" name="name" id="name" placeholder="Name" class="form-control">
</div>
</div>
<div class="row form-group">
<label class="col-2 col-form-label" for="subject">
Betreff:
</label>
<div class="col">
<select id="subject" name="subject" class="form-control">
<option>Bitte wählen</option>
<?php foreach($options as $value => $item):
$selectString = $item['selected']?' selected':'';
?>
<option value="<?= $value?>"<?=$selectString?>><?= $item['title']?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="row form-group">
<label class="col-2 col-form-label" for="message">
Nachricht:
</label>
<div class="col">
<textarea id="message" name="message" class="form-control" rows="3"><?= htmlspecialchars($message, ENT_QUOTES, 'UTF-8') ?></textarea>
</div>
</div>
<div class="row form-check">
<div class="offset-2 col">
<input type="checkbox" name="robot" id="robot" class="form-check-input"<?= $isRobot ? ' checked' : '' ?>>
<label for="robot" class="form-check-label">Hiermit bestätige ich, dass ich ein Roboter bin.</label>
</div>
</div>
</div>
<div class="card-footer">
<button class="btn btn-primary">Anfrage senden</button>
</div>
</form>
</div>
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment