0% found this document useful (0 votes)
7 views

PHP Handling Program

Uploaded by

hepsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

PHP Handling Program

Uploaded by

hepsi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP Handling Form (POST METHOD):

Form.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Simple PHP Form</title>

</head>

<body>

<h1>Contact Us</h1>

<form action="handle_form.php" method="POST">

<label for="name">Name:</label>

<input type="text" id="name" name="name" required><br><br>

<label for="email">Email:</label>

<input type="email" id="email" name="email" required><br><br>

<label for="message">Message:</label><br>

<textarea id="message" name="message" rows="4" cols="50" required></textarea><br><br>

<input type="submit" value="Submit">

</form>

</body>

</html>
Handle_form.php

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Sanitize and validate input data

$name = htmlspecialchars(trim($_POST['name']));

$email = htmlspecialchars(trim($_POST['email']));

$message = htmlspecialchars(trim($_POST['message']));

// Basic validation

if (empty($name) || empty($email) || empty($message)) {

echo "All fields are required.";

} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

echo "Invalid email format.";

} else {

// Process the data (e.g., save to a file or database, send an email)

// For this example, we'll just display a confirmation message

echo "<h1>Thank You!</h1>";

echo "<p>Name: " . $name . "</p>";

echo "<p>Email: " . $email . "</p>";

echo "<p>Message: " . $message . "</p>";

// Optional: Send an email

$to = '[email protected]'; // Replace with your email

$subject = 'Contact Form Submission';

$body = "Name: $name\nEmail: $email\nMessage: $message";

$headers = "From: $email";

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

echo "<p>Email sent successfully.</p>";


} else {

echo "<p>Failed to send email.</p>";

} else {

echo "Invalid request.";

?>

You might also like