Web forms are crucial elements of any website as they enable users to submit data that the server can process. In this tutorial, you will learn how to create a basic PHP web form that collects user inputs and handles them on the server side.

Understanding PHP Web Forms

Web forms are a medium between a user and a server that collects user requests and information. They are frequently used for account registration, login, feedback submission, and other purposes. A PHP web form comprises an HTML form interface and a PHP script that processes the form data. PHP is a good choice as it can manage form data securely and efficiently.

Building the Form

Creating a web form involves designing an intuitive user interface and setting up a server-side script to handle the data securely:

Designing the HTML Form

Creating the form's HTML structure involves designing a user-friendly layout and specifying the form fields:

<form action="submit.php" method="POST">
                <legend>Personal Information</legend>
                <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>

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

In the example above, the form data will be sent to submit.php using the POST method, ensuring that data is not visible in the URL.

Styling the Form

Use CSS to enhance the appearance of your form:

        body {
                font-family: 'Arial', sans-serif;
                line-height: 1.6;
                background-color: #fafafa;
                font-size: 16px;
                color: #333;
                padding: 20px;

        form {
                background: #fff;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                max-width: 400px;
                margin: 40px auto;

        fieldset {
                border: none;
                margin: 0;
                padding: 0;

        legend {
                font-size: 1.25em;
                margin-bottom: 10px;

        label {
                margin-bottom: 5px;

        input[type="email"] {
                width: calc(100% - 22px);
                /* Full width minus padding and border */
                padding: 10px;
                margin-bottom: 15px;
                border: 1px solid #ccc;
                border-radius: 4px;

        input[type="submit"] {
                background-color: #007BFF;
                color: white;
                padding: 10px 15px;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                transition: background-color 0.3s;

        input[type="submit"]:hover {
                background-color: #0056b3;

Processing Form Data with PHP

After the user submits the form, you need to handle the data on the server side. This is done in the submit.php file:

    // Collect and sanitize input data
    $name = htmlspecialchars(stripslashes(trim($_POST['name'])));
    $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);

    // Validate input data
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid email format";
    } else {
        echo "Thank you, " . $name . "! We have received your details.";

This above PHP script checks if the form was submitted using POST, sanitizes the inputs using PHP's built-in functions, and validates the email address.

Testing Your Form

  • Upload both your HTML file and submit.php to your server.
  • Access the HTML file through a browser, fill out the form, and press submit.
  • If configured correctly, submit.php will process and respond with the success message.

Best Practices

To enhance the security of your PHP form, consider the following practices:

  • Validation: Always validate and sanitize incoming data to prevent security vulnerabilities.
  • User feedback: Provide clear feedback for successful or unsuccessful submissions.
  • Accessibility: Ensure your form is accessible by using proper labels and semantic HTML.
  • Security: Use HTTPS to encrypt data transmissions and implement CSRF tokens to prevent unwanted actions on authenticated web sessions.


You have now learned how to build a simple PHP web form and handle its data securely and efficiently. This knowledge enables you to manage user input effectively in web applications, ensuring functionality and security. Depending on your needs, you can adjust this basic form to include more fields, use CAPTCHA for security, or interact with a database to store user inputs.

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram