website/pages/contact/contact.php
2023-05-11 20:01:54 +02:00

61 lines
1.8 KiB
PHP

<link rel="stylesheet" href="/css/contact.css">
<h1>Kontaktujte </h1>
<form class="form_contact" action="/contact/send" method="post" name="contact">
<label for="name">Vaše jméno:</label>
<input id="name" type="text" placeholder="Petr Novák" required autocomplete="on" />
<label for="email">Váš e-mail:</label>
<input id="email" type="email" placeholder="petr@novak.cz" required autocomplete="on" />
<label for="message">Zpráva:</label>
<textarea id="message" placeholder="Napište zprávu..." required></textarea>
<button type="button" id="submit">Odeslat zprávu</button>
</form>
<script>
const submit = document.querySelector("#submit")
submit.addEventListener("click", async (e) => {
e.preventDefault()
const formData = new FormData();
formData.append('name', document.querySelector("#name").value);
formData.append('email', document.querySelector("#email").value);
formData.append('message', document.querySelector("#message").value);
try {
await fetch('/contact/send', {
method: 'POST',
body: formData
});
console.log('Message sent successfully.')
} catch (err) {
console.error('Error:', error)
}
})
</script>
<?php
ini_set('log_errors', 1);
if(isset($_POST['email']) && isset($_POST['message'])) {
$email = $_POST['email'];
$message = $_POST['message'];
$name = $_POST['name'];
require_once(__DIR__ . "/../../libraries/mail.php");
$message = "Nová zpráva z webu filiprojek.cz\n\nOdesílatel: $email\nZpráva:\n$message";
$m = new Mail($_POST['email'], 'webmaster@filiprojek.cz', 'Nová zpráva z webu filiprojek.cz', $message);
if($m->send()) {
console_log("zprava odeslana");
header('Location: /?sent=true');
} else {
error_log("Message wasn't sent");
error_log(var_dump($m));
echo "<p>Něco se nepovedlo, zkuste to znovu... :(</p>";
echo "<a href='/'>Zpět</a>";
}
}