Woodman Woodman - 1 year ago 113
PHP Question

Trouble completing a simple contactform

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 = '[email protected]';
$subject = 'Reactie via website ZoetZoutZuur';


$body = "Afzender: $name\n E-mailadres: $email\n Bedrijfsnaam: $company\n
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>';
}

}
?>

Answer Source

Your ternary operator are wrong :

$name = isset($_POST['name']) ? check_input($_POST['name']);

should be

$name = isset($_POST['name']) ? check_input($_POST['name']) : "";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download