mail func added, post route added, more css

This commit is contained in:
Filip Rojek 2023-04-13 17:39:50 +02:00
parent b7721daa00
commit 8065ab134d
6 changed files with 99 additions and 12 deletions

View File

@ -18,6 +18,8 @@
.card { .card {
display: flex; display: flex;
border: 1px solid red; border: 1px solid red;
border-radius: 15px;
box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;
width: min(40rem, calc(100% - 1rem)); width: min(40rem, calc(100% - 1rem));
} }
@ -40,7 +42,7 @@
.technologies { .technologies {
display: flex; display: flex;
gap: 1rem; gap: 1rem;
margin-right: auto; flex-wrap: wrap;
} }
.technologies span { .technologies span {

View File

@ -3,6 +3,7 @@ session_start();
$prod = false; $prod = false;
require_once("./libraries/router.php"); require_once("./libraries/router.php");
require_once("./libraries/console.php");
// Display Errors // Display Errors
ini_set('display_startup_errors', 1); ini_set('display_startup_errors', 1);
@ -70,9 +71,11 @@ if($prod) { ?>
<section class="content"> <section class="content">
<?php <?php
$R = new Router(); $R = new Router();
$R->route('/', 'home'); $R->route('GET', '/', 'home');
$R->route('/home', 'home'); $R->route('GET', '/home', 'home');
$R->route('/domu', 'home'); $R->route('GET', '/domu', 'home');
$R->route('POST', '/contact/send', 'contact');
$R = null; $R = null;
?> ?>

9
libraries/console.php Normal file
View File

@ -0,0 +1,9 @@
<?php
function console_log($data) {
$output = $data;
if (is_array($output))
$output = implode(',', $output);
echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
}

31
libraries/mail.php Normal file
View File

@ -0,0 +1,31 @@
<?php
class Mail {
public $from;
public $to;
public $subject;
public $message;
public $headers;
function __construct($from, $to, $subject, $message, $headers=null) {
$this->from = $from;
$this->to = $to;
$this->subject = $subject;
$this->headers = $headers;
}
public function send() {
// odeslání e-mailu
$headers = 'From: noreply@fofrweb.com' . "\r\n" .
'Reply-To: support@fofrweb.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$this->headers = $headers;
if (gettype($this->message) == 'string') {
if(mail($this->to, $this->subject, $this->message, $this->headers)) {
return 0;
} else {
return 1;
}
}
}
}

View File

@ -2,8 +2,10 @@
class Router { class Router {
public $returned = false; public $returned = false;
function route($url, $filename) { function route($method, $url, $filename) {
if($_SERVER['REQUEST_METHOD'] == 'GET') { $methods = ['GET', 'POST'];
if(in_array($method, $methods)) {
if($_SERVER['REQUEST_METHOD'] == $method) {
if ($_SERVER['REQUEST_URI'] == $url) { if ($_SERVER['REQUEST_URI'] == $url) {
require_once("./pages/$filename/$filename.php"); require_once("./pages/$filename/$filename.php");
$this->returned = true; $this->returned = true;
@ -11,6 +13,7 @@ class Router {
} }
} }
} }
}
function __destruct() { function __destruct() {
if($_SERVER['REQUEST_METHOD'] == 'GET') { if($_SERVER['REQUEST_METHOD'] == 'GET') {

View File

@ -1,7 +1,7 @@
<link rel="stylesheet" href="/css/contact.css"> <link rel="stylesheet" href="/css/contact.css">
<h1>Kontaktujte </h1> <h1>Kontaktujte </h1>
<form class="form_contact" action="#" method="post" name="contact"> <form class="form_contact" action="/contact/send" method="post" name="contact">
<label for="name">Vaše jméno:</label> <label for="name">Vaše jméno:</label>
<input id="name" type="text" placeholder="Petr Novák" required autocomplete="on" /> <input id="name" type="text" placeholder="Petr Novák" required autocomplete="on" />
@ -11,6 +11,45 @@
<label for="message">Zpráva:</label> <label for="message">Zpráva:</label>
<textarea id="message" placeholder="Napište zprávu..." required></textarea> <textarea id="message" placeholder="Napište zprávu..." required></textarea>
<button type="submit">Odeslat zprávu</button> <button type="button" id="submit">Odeslat zprávu</button>
</form> </form>
<script>
const submit = document.querySelector("#submit")
submit.addEventListener("click", (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 {
fetch('/contact/send', {
method: 'POST',
body: formData
});
console.log('Message sent successfully.')
} catch (err) {
console.error('Error:', error)
}
})
</script>
<?php
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 Fofrweb.com\n\nOdesílatel: $email\nZpráva:\n$message";
$m = new Mail($_POST['email'], 'tym@fofrweb.com', 'Nová zpráva z webu Fofrweb', $message);
if($m->send()) {
header('Location: /?sent=true');
} else {
echo "<p>Něco se nepovedlo, zkuste to znovu... :(</p>";
echo "<a href='/'>Zpět</a>";
}
}