My deliberately kept-simple contact form works without errors when uploaded to my webspace, but XAMPP throws several "Notice: Undefined variable" warnings.
Is this to be expected?
This is the entire code from index.php:
- Code: Select all
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'example@mail.com';
$subject = 'Message from Contact Demo';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name'])
{
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message'])
{
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage)
{
if (mail ($to, $subject, $body, $from))
{
$result='<div>Thank You!</div>';
}
else
{
$result='<div>Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact Form With PHP Example</title>
</head>
<body>
<form action="index.php" method="post">
<label for="name">Name</label>
<input type="text" id="name" name="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p>$errName</p>";?>
<label for="email">Email</label>
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p>$errEmail</p>";?>
<label for="message">Message</label>
<textarea rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p>$errMessage</p>";?>
<input type="submit" id="submit" name="submit" value="Send" class="btn btn-primary">
<?php echo $result; ?>
</form>
</body>
</html>
Thank you!
Chris