Hi guys,
I'm a total newbie to php and have tried so many things to create a php script that proces my form on my website and sends me an e-mail when people put in ther information..... It's not totally working. Maybe you can help me?
The forms html code is:
and my php script kontaktform.php has this code (so far) -
The script sends information to my e-mail, but there are things left to sort out that I hope you can help me with -
1) the form doesn't send information from checkboxes
2) As I can read - I need some validation for the form, so it's secure
I really hope you can help me - I'm pretty stuck on this.
/ Jakob
I'm a total newbie to php and have tried so many things to create a php script that proces my form on my website and sends me an e-mail when people put in ther information..... It's not totally working. Maybe you can help me?
The forms html code is:
HTML:
<div class="row">
<div class="full">
<hr class="line30">
<h2>BESTIL TILBUD VIA FORMULAR</h2>
<div class="container" style="text-align:left;">
<form action="kontaktform.php" method="post">
<label for="name">Dit navn</label>
<input type="text" id="name" name="name" placeholder="Dit navn..." required>
<label for="mail">E-mail</label>
<input type="text" id="email" name="email" placeholder="[email protected]" required>
<label for="phone">Telefonnummer</label>
<input type="text" id="phone" name="phone" placeholder="12345678" required>
<label for="place"><b>Jeg ønsker at...</b></label><br><br>
<input type="checkbox" id="tilbudillustrationer" name="tilbudillustrationer" value="tilbudillustrationer">
<label for="tilbudillustrationer" style="margin-right:20px;">Bestille tilbud på illustrationer</label><br><br>
<input type="checkbox" id="bestilling" name="bestilling" value="bestilling" style="padding:20px;">
<label for="bestillingsopgave" style="margin-right:20px;">Snakke om en bestillingsopgave</label><br><br>
<input type="checkbox" id="ide" name="ide" value="ide" style="padding:20px;">
<label for="ide" style="margin-right:20px;">Jeg ønsker at drøfte en idé</label><br><br>
<input type="checkbox" id="andet" name="andet" value="andet" style="padding:20px;">
<label for="andet" style="margin-right:20px;">Andet</label><br><br><br>
<label for="about">Emne</label>
<textarea id="about" name="about" placeholder="Min henvendelse drejer sig om..." style="height:200px"></textarea>
<input type="submit" value="Send">
</form>
</div>
</div>
</div>
and my php script kontaktform.php has this code (so far) -
PHP:
<?php
$errors = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get POST data
$name = isset($_POST['name']) ? strip_tags(trim($_POST['name'])) : '';
$email = isset($_POST['email']) ? trim($_POST['email']) : '';
$phone = isset($_POST['phone']) ? trim($_POST['phone']) : '';
$tilbudillustrationer = isset($_POST['tilbudillustrationer']) ? trim($_POST['tilbudillustrationer']) : '';
$bestilling = isset($_POST['bestilling']) ? trim($_POST['bestilling']) : '';
$ide = isset($_POST['ide']) ? trim($_POST['ide']) : '';
$andet = isset($_POST['andet']) ? trim($_POST['andet']) : '';
$about = isset($_POST['about']) ? trim($_POST['about']) : '';
$email = isset($_POST['email']) ? strip_tags(trim($_POST['email'])) : '';
// If no errors, send email
if (empty($errors)) {
// Recipient email address (replace with your own)
$recipient = "[email protected]";
// Additional headers
$headers = "From: <$name> Mail: <$email> Telefon: <$phone> Besked: <$about>";
// Send email
if (mail($recipient, $message, $headers)) {
echo "<span style=padding:30px;font-size:24px;font-family:georgia, serif;>Tak for e-mailen, jeg svarer så hurtigt jeg kan | Thank you for the email, I will reply as soon as I can </span><br><br><span style=padding:30px;font-size:20px;font-family:georgia, serif;><b>- Jakob i</b></span><br><br><br><span style=padding:30px;font-size:20px;font-family:georgia, serif;><a style=color:blue;cursor:pointer; onclick=history.back()>Tilbage >></a></span>";
} else {
echo "Failed to send email. Please try again later.";
}
} else {
// Display errors
echo "The form contains the following errors:<br>";
foreach ($errors as $error) {
echo "- $error<br>";
}
}
} else {
// Not a POST request, display a 403 forbidden error
header("HTTP/1.1 403 Forbidden");
echo "You are not allowed to access this page.";
}
?>
The script sends information to my e-mail, but there are things left to sort out that I hope you can help me with -
1) the form doesn't send information from checkboxes
2) As I can read - I need some validation for the form, so it's secure
I really hope you can help me - I'm pretty stuck on this.
/ Jakob