PHP forms example
Dear student; You can write and save HTML and PHP codes with different file name as
instructed or You can code them in one file by changing file name.
1. Simple Contact Form
HTML Form:
<form action="contact.php" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
<input type="submit" value="Send Message">
</form>
PHP Handling (contact.php):
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
// Send email (you'll need a configured mail server)
$to = '
[email protected]';
$subject = 'New Message from Contact Form';
$body = "Name: $name\nEmail: $email\nMessage:\n$message";
$headers = "From: $email";
if (mail($to, $subject, $body, $headers)) {
echo "Message sent successfully!";
} else {
echo "Error sending message.";
}
}
?>
2. User Registration Form
HTML Form:
<form action="register.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<input type="submit" value="Register">
</form>
PHP Handling please name it register.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = htmlspecialchars($_POST['username']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$email = htmlspecialchars($_POST['email']);
// Save to database (assuming $conn is your database connection)
$stmt = $conn->prepare("INSERT INTO users (username, password, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $username, $password, $email);
if ($stmt->execute()) {
echo "Registration successful!";
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
}
?>
3. Login Form
HTML Form:
<form action="login.php" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Login">
</form>
PHP Handling named as login.php:
<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = htmlspecialchars($_POST['username']);
$password = $_POST['password'];
// Fetch user from database (assuming $conn is your database connection)
$stmt = $conn->prepare("SELECT password FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($hashed_password);
$stmt->fetch();
if (password_verify($password, $hashed_password)) {
$_SESSION['username'] = $username;
echo "Login successful! Welcome, " . $username;
} else {
echo "Invalid username or password.";
}
$stmt->close();
}
?>
4. File Upload Form
HTML Form
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Choose file:</label>
<input type="file" id="file" name="file" required>
<input type="submit" value="Upload">
</form>
PHP Handling named as upload.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
// Check file size
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars(basename($_FILES["file"]["name"])) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}
?>