I am a novice coder currently working on my very first php project. A website has a simple php contactform I'm trying to complete but I keep running into unexpected errors. I have tried many things now that I found on Stackoverflow and solved some errors myself, but I am at a loss right now. I hope someone would review my code to help me see what I'm missing here. Any help to get me back on track is much appreciated.
edit: tried to make this post more on topic
What I want this script to do:
- make name, email and message required fields
- make form secure against malicious intent. That is why I added the check_input function, following Securing a contact form script
Complete code as of now:
<?php
$name = isset($_POST['name']) ? check_input($_POST['name']) : "";
$email = isset($_POST['email']) ? check_input($_POST['email']) : "";
$company = isset($_POST['company']) ? check_input($_POST['company']) : "";
$message = isset($_POST['message']) ? check_input($_POST['message']) : "";
$from = 'From: ZZZ Reactieformulier';
$to = 'emailadres@mailserver.nl';
$subject = 'Reactie via website';
$body = "Afzender: $name
E-mailadres: $email
Bedrijfsnaam: $company
Bericht: $message";
function check_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_POST['submit']) {
if ($name !='' && $email !='' && $message !='') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Uw bericht is succesvol verzonden!</p>';
} else {
echo '<p>Er is iets misgegaan, probeer het alstublieft
opnieuw</p>';
}
} else {
echo '</p>De velden Naam, E-mail en Bericht zijn verplicht</p>';
}
}
?>