Web20 University

Creating a Fully Functional Form - An example of a PHP Full Form

Get Unlimited Bandwidth and FREE Domain Names for All Your PHP Projects!

PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language that is especially suited for web development. One of the most common tasks in web development is handling user input through forms. In this blog post, we’ll dive into creating a fully functional form using PHP, covering both the frontend HTML form and the backend PHP script to process the form data.

What is PHP Full Form?

In this context, “PHP full form” refers to a complete and functional PHP form, encompassing both the HTML form structure and the PHP script required to process and validate the input.

Step-by-Step Guide to Creating a PHP Full Form

Let’s create a simple contact form where users can submit their name, email, and a message. The form will include both client-side and server-side validations.

Step 1: Create the HTML Form

First, we need to create an HTML file that contains the form. Save this file as contact.html.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact Us</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 50px; }
        form { max-width: 600px; margin: auto; }
        label { display: block; margin-top: 10px; }
        input, textarea { width: 100%; padding: 10px; margin-top: 5px; }
        button { padding: 10px 15px; margin-top: 10px; background-color: #28a745; color: #fff; border: none; cursor: pointer; }
        button:hover { background-color: #218838; }
    </style>
</head>
<body>
    <h1>Contact Us</h1>
    <form action="process_form.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" rows="5" required></textarea>
        
        <button type="submit">Send Message</button>
    </form>
</body>
</html>

Step 2: Create the PHP Script to Process the Form

Next, create a PHP file named process_form.php to handle the form submission.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Get form inputs
    $name = htmlspecialchars(trim($_POST['name']));
    $email = htmlspecialchars(trim($_POST['email']));
    $message = htmlspecialchars(trim($_POST['message']));

    // Validate inputs
    if (empty($name) || empty($email) || empty($message)) {
        echo "All fields are required.";
        exit;
    }

    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid email format.";
        exit;
    }

    // Prepare the email
    $to = "[email protected]"; // Replace with your email
    $subject = "Contact Form Submission from $name";
    $body = "Name: $name\nEmail: $email\n\nMessage:\n$message";
    $headers = "From: $email";

    // Send the email
    if (mail($to, $subject, $body, $headers)) {
        echo "Thank you for your message. We will get back to you soon.";
    } else {
        echo "Sorry, there was an error sending your message. Please try again later.";
    }
} else {
    echo "Invalid request method.";
}
?>

Explanation of the PHP Script

  1. Form Submission Check: The script begins by checking if the form was submitted using the POST method.
  2. Sanitization and Validation: It sanitizes and trims the inputs to remove any harmful characters and whitespace. It also validates the email format.
  3. Email Preparation: The script sets up the email details, including the recipient’s email, subject, body, and headers.
  4. Email Sending: The PHP mail function is used to send the email. It provides feedback to the user based on the success or failure of the email sending process.

Step 3: Enhancing Security

To further enhance the security of your form, consider the following measures:

  1. CAPTCHA: Implement a CAPTCHA to prevent automated spam submissions.
  2. CSRF Protection: Use tokens to protect against Cross-Site Request Forgery attacks.
  3. Input Sanitization: Use more comprehensive sanitization and validation functions to ensure the integrity of the data.
  4. HTTPS: Ensure that your form and server use HTTPS to encrypt the data transmitted between the client and server.

Conclusion

Creating a PHP full form involves setting up an HTML form and a corresponding PHP script to handle and process the form data. This guide has walked you through creating a simple contact form, complete with client-side and server-side validation. By following these steps, you can easily implement a contact form on your website, allowing users to reach out to you securely and effectively.

Remember, while this example covers the basics, always consider additional security measures to protect both your application and your users’ data.

Get Unlimited Bandwidth and FREE Domain Names for All Your PHP Projects!