Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

<?

php
// Check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the input values
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];

// Validate input (you can add more validation)


if (empty($name) || empty($email) || empty($message)) {
$error = "All fields are required.";
} else {
// Send email
$to = "your@email.com";
$subject = "Contact Form Submission";
$body = "Name: $name\nEmail: $email\n\n$message";

if (mail($to, $subject, $body)) {


$success = "Your message has been sent successfully.";
} else {
$error = "Oops! Something went wrong. Please try again later.";
}
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Form</title>
<style>
/* Add your CSS styles here */
</style>
</head>
<body>
<div class="container">
<h2>Contact Me</h2>
<?php if(isset($error)) { ?>
<div class="error"><?php echo $error; ?></div>
<?php } ?>
<?php if(isset($success)) { ?>
<div class="success"><?php echo $success; ?></div>
<?php } ?>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required>
<label for="message">Message:</label>
<textarea name="message" id="message" required></textarea>
<button type="submit">Send Message</button>
</form>
</div>
</body>
</html>

You might also like