PHP Forms

Collect and process user input through HTML forms

📝 What are PHP Forms?

PHP forms collect user input through HTML form elements and process it on the server. They enable user interaction, data submission, registration, login, and communication between users and web applications.


<form method="POST" action="">
    <input type="text" name="username">
    <button type="submit">Submit</button>
</form>

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "Hello, " . $_POST['username'];
}
?>
                                    

Form Elements

📮

Form Methods

GET sends data through URL parameters (visible), while POST sends data in request body (hidden). Use POST for sensitive data like passwords.

<form method="POST">
<form method="GET">
✏️

Input Fields

Text inputs collect single-line data like names, emails, and passwords. Use different input types for validation and better user experience on mobile devices.

<input type="text" name="username">
<input type="email" name="email">
📋

Textarea

Textareas allow multi-line input for longer content like comments, messages, or descriptions. They automatically expand to show all content entered by users.

<textarea name="message" rows="4">
</textarea>
☑️

Checkboxes & Radio

Checkboxes allow multiple selections while radio buttons allow only one choice. Perfect for surveys, preferences, and multiple-choice questions in forms.

<input type="checkbox" name="agree">
<input type="radio" name="gender">
📂

Select Dropdown

Dropdown menus save space and provide predefined options. Great for country selection, categories, or any list with many choices to keep forms clean.

<select name="country">
    <option value="us">USA</option>
</select>
📁

File Upload

File inputs allow users to upload documents, images, or other files. Remember to set enctype="multipart/form-data" on the form for file uploads to work.

<input type="file" name="upload">

🔹 Basic Form with POST

Create a simple form that submits data using POST method:

<!DOCTYPE html>
<html>
<body>

<form method="POST" action="">
    <label>Name:</label>
    <input type="text" name="name" required>
    <br><br>
    
    <label>Email:</label>
    <input type="email" name="email" required>
    <br><br>
    
    <button type="submit" name="submit">Submit</button>
</form>

<?php
if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    
    echo "<h3>Form Submitted!</h3>";
    echo "Name: $name<br>";
    echo "Email: $email";
}
?>

</body>
</html>

Output (after submission):

Form Submitted!

Name: John Doe

Email: [email protected]

🔹 Form with GET Method

GET method sends data through URL parameters:

<form method="GET" action="">
    <label>Search:</label>
    <input type="text" name="query" placeholder="Enter search term">
    <button type="submit">Search</button>
</form>

<?php
if(isset($_GET['query'])) {
    $search = $_GET['query'];
    echo "<p>You searched for: <strong>$search</strong></p>";
    // URL will be: page.php?query=search_term
}
?>

Output:

You searched for: PHP tutorials

URL: page.php?query=PHP+tutorials

🔹 Textarea and Multiple Lines

Collect longer text input using textarea:

<form method="POST" action="">
    <label>Your Message:</label><br>
    <textarea name="message" rows="5" cols="40" placeholder="Enter your message..."></textarea>
    <br><br>
    <button type="submit" name="submit">Send</button>
</form>

<?php
if(isset($_POST['submit'])) {
    $message = $_POST['message'];
    echo "<h4>Your Message:</h4>";
    echo "<p>" . nl2br($message) . "</p>"; // nl2br converts newlines to <br>
}
?>

Output:

Your Message:

Hello!
This is a multi-line message.
Thank you!

🔹 Checkboxes

Allow users to select multiple options:

<form method="POST" action="">
    <label>Select your hobbies:</label><br>
    <input type="checkbox" name="hobbies[]" value="Reading"> Reading<br>
    <input type="checkbox" name="hobbies[]" value="Gaming"> Gaming<br>
    <input type="checkbox" name="hobbies[]" value="Sports"> Sports<br>
    <input type="checkbox" name="hobbies[]" value="Music"> Music<br><br>
    <button type="submit" name="submit">Submit</button>
</form>

<?php
if(isset($_POST['submit'])) {
    if(isset($_POST['hobbies'])) {
        echo "<h4>Your hobbies:</h4>";
        foreach($_POST['hobbies'] as $hobby) {
            echo "- $hobby<br>";
        }
    } else {
        echo "No hobbies selected.";
    }
}
?>

Output:

Your hobbies:

- Reading

- Gaming

- Music

🔹 Radio Buttons

Allow users to select only one option:

<form method="POST" action="">
    <label>Select your gender:</label><br>
    <input type="radio" name="gender" value="Male"> Male<br>
    <input type="radio" name="gender" value="Female"> Female<br>
    <input type="radio" name="gender" value="Other"> Other<br><br>
    <button type="submit" name="submit">Submit</button>
</form>

<?php
if(isset($_POST['submit'])) {
    if(isset($_POST['gender'])) {
        $gender = $_POST['gender'];
        echo "Selected gender: <strong>$gender</strong>";
    } else {
        echo "Please select a gender.";
    }
}
?>

Output:

Selected gender: Male

🔹 Select Dropdown

Create dropdown menus for selection:

<form method="POST" action="">
    <label>Select your country:</label>
    <select name="country">
        <option value="">-- Choose --</option>
        <option value="USA">United States</option>
        <option value="UK">United Kingdom</option>
        <option value="Canada">Canada</option>
        <option value="Australia">Australia</option>
    </select>
    <br><br>
    <button type="submit" name="submit">Submit</button>
</form>

<?php
if(isset($_POST['submit'])) {
    if(!empty($_POST['country'])) {
        $country = $_POST['country'];
        echo "Selected country: <strong>$country</strong>";
    } else {
        echo "Please select a country.";
    }
}
?>

Output:

Selected country: Canada

🔹 Multiple Select

Allow multiple selections from dropdown:

<form method="POST" action="">
    <label>Select programming languages:</label><br>
    <select name="languages[]" multiple size="4">
        <option value="PHP">PHP</option>
        <option value="JavaScript">JavaScript</option>
        <option value="Python">Python</option>
        <option value="Java">Java</option>
    </select>
    <br><small>Hold Ctrl to select multiple</small><br><br>
    <button type="submit" name="submit">Submit</button>
</form>

<?php
if(isset($_POST['submit'])) {
    if(isset($_POST['languages'])) {
        echo "<h4>Selected languages:</h4>";
        foreach($_POST['languages'] as $lang) {
            echo "- $lang<br>";
        }
    }
}
?>

Output:

Selected languages:

- PHP

- JavaScript

- Python

🔹 Practical Example: Registration Form

Complete registration form with multiple input types:

<form method="POST" action="" style="max-width: 400px;">
    <h3>User Registration</h3>
    
    <label>Full Name:</label>
    <input type="text" name="fullname" required><br><br>
    
    <label>Email:</label>
    <input type="email" name="email" required><br><br>
    
    <label>Password:</label>
    <input type="password" name="password" required><br><br>
    
    <label>Gender:</label>
    <input type="radio" name="gender" value="Male"> Male
    <input type="radio" name="gender" value="Female"> Female<br><br>
    
    <label>Country:</label>
    <select name="country">
        <option value="USA">USA</option>
        <option value="UK">UK</option>
        <option value="Canada">Canada</option>
    </select><br><br>
    
    <input type="checkbox" name="terms" required> I agree to terms<br><br>
    
    <button type="submit" name="register">Register</button>
</form>

<?php
if(isset($_POST['register'])) {
    echo "<h3>Registration Successful!</h3>";
    echo "Name: " . $_POST['fullname'] . "<br>";
    echo "Email: " . $_POST['email'] . "<br>";
    echo "Gender: " . $_POST['gender'] . "<br>";
    echo "Country: " . $_POST['country'];
}
?>

💡 Form Best Practices:

  • Use POST for sensitive data: Passwords, personal information
  • Validate input: Always check and sanitize user input
  • Use required attribute: Make important fields mandatory
  • Provide feedback: Show success or error messages
  • Use labels: Associate labels with input fields for accessibility
  • Check isset(): Verify form data exists before processing

🧠 Test Your Knowledge

Which form method should you use for sensitive data like passwords?