I am trying to create a form that checks and validates name, email. But I can't see any error messages. I don't know a lot of PHP, can't say that I even know the basics.
Here is the code:
<iframe name="formDestination" class="nnn"></iframe>
<div class="container33">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="thwid" method="post" target="formDestination">
<label for="fname">Full Name</label>
<input type="text" id="fname" name="firstname" placeholder="Your full name..." value="<?php echo $name;?>"><span class="error">* <?php echo $nameErr;?></span>
<label for="email">Your E-mail</label>
<input type="text" id="email" name="email" placeholder="Your E-mail adress..."> <span class="error">* <?php echo $emailErr;?></span>
<label for="message">Your message</label>
<textarea id="message" name="message" placeholder="Write your message here / the reason why you want to contact us " ></textarea>
<input type="submit" name="submit" value="Submit">
</form>
</div>
<?php
if(isset($_POST['submit'])){
$to = "[email protected]";
$from = $_POST['email'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2);
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
} ?>
<?php
$nameErr = $emailErr = "";
$name = $email = $comment = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}?>