Hello i am trying to do a contact form that gives me an alert box when i press the submit button (e.g. one alert box for "form could not be submitted" and one for "form was successfully submitted") How can i do this? Does it work with if/else ?
I worked with html/php/js for the form, below is my code (sorry for german)
$anrede = $vorname = $nachname = $anschrift = $postleitzahl = $stadt = $email = $telefon = $nachricht = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["anrede"])) {
$anredeErr = "* Anrede wird benötigt";
} else {
$anrede = test_input($_POST["anrede"]);
}
if (empty($_POST["vorname"])) {
$vornameErr = "* Vorname wird benötigt";
} else {
$vorname = test_input($_POST["vorname"]);
if (!preg_match("/^[a-zA-Z]*$/",$vorname)) {
$vornameErr = "* Nur Buchstaben erlaubt";
}
}
if (empty($_POST["nachname"])) {
$nachnameErr = "* Nachname wird benötigt";
} else {
$nachname = test_input($_POST["nachname"]);
if (!preg_match("/^[a-zA-Z]*$/",$nachname)) {
$nachnameErr = "* Nur Buchstaben erlaubt";
}
}
if (empty($_POST["anschrift"])) {
$anschriftErr = "* Anschrift wird benötigt";
} else {
$anschrift = test_input($_POST["anschrift"]);
}
if (empty($_POST["postleitzahl"])) {
$postleitzahlErr = "* Postleitzahl wird benötigt";
} else {
$postleitzahl = test_input($_POST["postleitzahl"]);
if (!preg_match("/^[0-9]*$/",$postleitzahl)) {
$postleitzahlErr = "* Nur Zahlen erlaubt";
}
}
if (empty($_POST["stadt"])) {
$stadtErr = "* Stadt / Ort wird benötigt";
} else {
$stadt = test_input($_POST["stadt"]);
if (!preg_match("/^[a-zA-Z-' ]*$/",$stadt)) {
$stadtErr = "* Nur Buchstaben und Leerzeichen erlaubt";
}
}
if (empty($_POST["email"])) {
$emailErr = "* E-Mail wird benötigt";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "* Falsches E-Mail Format";
}
}
if (empty($_POST["telefon"])) {
$telefon = "";
} else {
$telefon = test_input($_POST["telefon"]);
if (!preg_match("/^[0-9]*$/",$telefon)) {
$telefonErr = "* Nur Zahlen erlaubt";
}
}
if (empty($_POST["nachricht"])) {
$nachrichtErr = "Nachricht wird benötigt";
} else {
$nachricht = test_input($_POST["nachricht"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="kontaktformular-container">
<h2 class="h2-body3">Schreiben Sie uns eine E-Mail.<br></h2>
<h1 class="h1-body3">Kontaktformular</h1>
<form class="kontaktformular" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="anrede" name="anrede" placeholder="Anrede *" value="<?php echo $anrede ?>">
<span class="error"><?php echo $anredeErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="vorname" name="vorname" placeholder="Vorname *" value="<?php echo $vorname ?>">
<span class="error"><?php echo $vornameErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="nachname" name="nachname" placeholder="Nachname *" value="<?php echo $nachname ?>">
<span class="error"><?php echo $nachnameErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="anschrift" name="anschrift" placeholder="Anschrift *" value="<?php echo $anschrift ?>">
<span class="error"><?php echo $anschriftErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="postleitzahl" name="postleitzahl" placeholder="Postleitzahl *" value="<?php echo $postleitzahl ?>">
<span class="error"><?php echo $postleitzahlErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="stadt" name="stadt" placeholder="Stadt / Ort *" value="<?php echo $stadt ?>">
<span class="error"><?php echo $stadtErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="email" name="email" placeholder="E-Mail-Adresse *" value="<?php echo $email ?>">
<span class="error"><?php echo $emailErr;?></span>
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<input class="form-input" type="text" id="telefon" name="telefon" placeholder="Telefon" value="<?php echo $telefon ?>">
<br>
</div>
<div class="form-div">
<i class="fas fa-pen"></i>
<textarea class="form-input" id="nachricht" name="nachricht" rows="10" placeholder="Ihre Nachricht *"><?php echo $nachricht ?></textarea>
<span class="error"><?php echo $nachrichtErr;?></span>
<br>
</div>
<br>
<p class="p-kontakt">Mit einem * markierte Felder sind Pflichtfelder.</p>
<br>
<p class="p-kontakt">Mit dem Absenden des Formulares akzeptieren Sie die <span class="datenschutz"><a class="a-kontakt" href="#">Bestimmungen zum<br>Datenschutz</a></span> und willigen ein von uns per E-Mail/Telefon kontaktiert zu werden.</p>
<br>
<input class="form-submit" onclick="submitted()" type="submit" name="anfrage absenden" value="Anfrage absenden">
<script>
function submitted() {
alert("Nachricht erfolgreich abgeschickt!");
}
</script>```